js如何实现一个div左右旋转
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js如何实现一个div左右旋转相关的知识,希望对你有一定的参考价值。
js控制div旋转的例子:<DIV id=aDiv style="WIDTH: 70px; POSITION: absolute; HEIGHT: 70px">
< IMG width=70 height=70 src="http://www.smallrain.net/jsimg/images/pic.gif" />//假如这个图
<DIV>
//使角度转起来
var angle = 0;
function doRotate()
//检查并确保角度值在0到360之间
if(angle>360) angle-=360;
//使角度增加
angle+=15;
//do rotation
rotate("aDiv",angle);
//定位旋转中心
var el = document.getElementByIdx_x_x("aDiv")//C#中要马上设位置.
el.style.top = 25 - (el.offsetHeight/2);//父元素的高度/2-旋转元素高度/2,还需注意旋转是同父元素的LOP,和left上关的,请上机测试
el.style.left = 25 - (el.offsetWidth/2);//父元素的宽度/2-旋转元素度/2
//循环
setTimeout("doRotate();",20);
参考技术A <!DOCTYPE HTML>
<html>
<head>
<meta charset=UTF-8>
<title>YuGiOh</title>
<style type="text/css">
#div
position: absolute;
top: 50px;
left: 300px;
width: 300px;
height: 300px;
line-height: 300px;
text-align: center;
border: 1px solid black;
</style>
<script type="text/javascript">
var rotateHTML5 = function (limit)
var reg = /(rotate\\([\\-\\+]?((\\d+)(deg))\\))/i;
var wt = div.style['-webkit-transform'], wts = wt.match (reg);
var $2 = RegExp.$2;
console.log ($2);
div.style['-webkit-transform'] = wt.replace ($2, parseFloat (RegExp.$3) + limit + RegExp.$4);
var rotateIE = function (obj)
var d = !!obj.d ? obj.d : 1;
var r = d * Math.PI / 180;
costheta = Math.cos (r);
sintheta = Math.sin (r);
obj.style.filter = "progid:DXImageTransform.Microsoft.Matrix()";
var item = obj.filters.item (0);
var width = obj.clientWidth;
var height = obj.clientHeight;
item.DX = -width / 2 * costheta + height / 2 * sintheta + width / 2;
item.DY = -width / 2 * sintheta - height / 2 * costheta + height / 2;
item.M11 = costheta;
item.M12 = -sintheta;
item.M21 = sintheta;
item.M22 = costheta;
obj.timer = setTimeout (function ()
var dx = d + 1;
dx = dx > 360 ? 1 : dx;
obj.d = dx;
rotate (obj, dx);
, 30);
;
var start = function ()
if (!!div.interval)
clearInterval (div.interval);
delete div.interval;
else
div.interval = setInterval (function ()
/.*webkit.*/i.test (navigator.userAgent) ? rotateHTML5 (1) : rotateIE (div);
, 30);
</script>
</head>
<body>
<button onclick="start();">rotate</button>
<div id="div" style="border-radius: 90px; -webkit-transform: rotate(10deg);">ROTATE</div>
</body>
</html> 参考技术B <div>标签本身没有动态属性的,要实现动态可以通过dom修改<div>内部引用的标签属性
JS实现用键盘控制DIV上下左右+放大缩小与变色
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>用键盘控制DIV</title> <style type="text/css"> html,body{overflow:hidden;} body{margin:0;padding:0;} pre{ color:green; padding:10px 15px; background:#f0f0f0; border:1px dotted #333; font:12px/1.5 Courier New; margin:12px; } span{color:#999;} #box{ position:absolute; top:50px; left:300px; width:100px; height:100px; background:red; } </style> <script type="text/javascript"> window.onload = function () { var oBox = document.getElementById("box"); var bLeft = bTop = bRight = bBottom = bCtrlKey = false; setInterval(function (){ if (bLeft){ oBox.style.left = oBox.offsetLeft - 10 + "px" }else if (bRight) { oBox.style.left = oBox.offsetLeft + 10 + "px" } if (bTop){ oBox.style.top = oBox.offsetTop - 10 + "px" }else if(bBottom){ oBox.style.top = oBox.offsetTop + 10 + "px" } //防止溢出 limit(); },30); document.onkeydown = function (event) { var event = event || window.event; bCtrlKey = event.ctrlKey; switch (event.keyCode) { case 37: bLeft = true; break; case 38: //放大 if(bCtrlKey){ var oldWidth = oBox.offsetWidth; var oldHeight = oBox.offsetHeight; oBox.style.width = oBox.offsetWidth * 1.5 + "px"; oBox.style.height = oBox.offsetHeight * 1.5 + "px"; oBox.style.left = oBox.offsetLeft - (oBox.offsetWidth - oldWidth) / 2 + "px"; oBox.style.top = oBox.offsetTop - (oBox.offsetHeight - oldHeight) / 2 + "px"; break; } bTop = true; break; case 39: bRight = true; break; case 40: if(bCtrlKey) { var oldWidth = oBox.offsetWidth; var oldHeight = oBox.offsetHeight; oBox.style.width = oBox.offsetWidth * 0.75 + "px"; oBox.style.height = oBox.offsetHeight * 0.75 + "px"; oBox.style.left = oBox.offsetLeft - (oBox.offsetWidth - oldWidth) / 2 + "px"; oBox.style.top = oBox.offsetTop - (oBox.offsetHeight - oldHeight) / 2 + "px"; break; } bBottom = true; break; case 49: bCtrlKey && (oBox.style.background = "green"); break; case 50: bCtrlKey && (oBox.style.background = "yellow"); break; case 51: bCtrlKey && (oBox.style.background = "blue"); break; } return false }; document.onkeyup = function (event) { switch ((event || window.event).keyCode){ case 37: bLeft = false; break; case 38: bTop = false; break; case 39: bRight = false; break; case 40: bBottom = false; break; } }; //防止溢出 function limit() { var doc = [document.documentElement.clientWidth, document.documentElement.clientHeight] //防止左侧溢出 oBox.offsetLeft <=0 && (oBox.style.left = 0); //防止顶部溢出 oBox.offsetTop <=0 && (oBox.style.top = 0); //防止右侧溢出 doc[0] - oBox.offsetLeft - oBox.offsetWidth <= 0 && (oBox.style.left = doc[0] - oBox.offsetWidth + "px"); //防止底部溢出 doc[1] - oBox.offsetTop - oBox.offsetHeight <= 0 && (oBox.style.top = doc[1] - oBox.offsetHeight + "px") } }; </script> </head> <body> <pre> 红色方块为键盘操作区域,您可以进行如下操作: 上:↑ 下:↓ 左:← 右:→ Ctrl + 1 : 背景变为绿色 Ctrl + 2 : 背景变为黄色 Ctrl + 3 : 背景变为蓝色 Ctrl + ↑ : 放大 Ctrl + ↓ : 缩小 </pre> <div id="box"></div> </body> </html>
摘自:http://www.jb51.net/article/32823.htm
以上是关于js如何实现一个div左右旋转的主要内容,如果未能解决你的问题,请参考以下文章
四个div,单击箭头左右移动,怎么可以实现自动轮播。单击一次移动 一个div??