JS技术

js 旋转图片特效

字号+ 作者: 来源: 2014-11-16 22:20 我要评论( )

用js写的图片旋转特效,兼容各主流浏览器

CSS 部份:
#demo { cursor:pointer; position:absolute;
filter:PRogid:DXImageTransform.Microsoft.Matrix( sizingmethod="auto expand" );}
javascript 部份:
var Img = function() {
    var $ = function(id) { return document.getElementById(id); }
    var ua = navigator.userAgent,
        isIE = /msie/i.test(ua) && !window.opera;
    var i = 0, sinDeg = 0, cosDeg = 0, timer = null ;
    var rotate = function(target, degree) {
        target = $(target);
        var orginW = target.clientWidth, orginH = target.clientHeight;
            clearInterval(timer);
        function run(angle) {
            if (isIE) { // IE
                cosDeg = Math.cos(angle * Math.PI / 180);
                sinDeg = Math.sin(angle * Math.PI / 180);
                with(target.filters.item(0)) {
                    M11 = M22 = cosDeg; M12 = -(M21 = sinDeg); 
                }
                target.style.top = (orginH - target.offsetHeight) / 2 + 'px';
                target.style.left = (orginW - target.offsetWidth) / 2 + 'px';
            } else if (target.style.MozTransform !== undefined) {  // Mozilla
                target.style.MozTransform = 'rotate(' + angle + 'deg)';
            } else if (target.style.OTransform !== undefined) {   // Opera
                target.style.OTransform = 'rotate(' + angle + 'deg)';
            } else if (target.style.webkitTransform !== undefined) { // Chrome Safari
                target.style.webkitTransform = 'rotate(' + angle + 'deg)';
            } else {
                target.style.transform = "rotate(" + angle + "deg)";
            }
        }
 
        timer = setInterval(function() {
            i += 30;
            run(i);
            if (i > degree - 1) {
                i = 0;
                clearInterval(timer);
            } 
        }, 10); 
    }
    return {rotate: rotate}
}();
window.onload = function() {
   // Img.rotate('demo', 360);
    document.getElementById('demo').onmouseover = function() {
        Img.rotate('demo', 360);
    }
}
html 部份:
<div id="container" style="width:20px;height:20px;position:relative;margin:0 auto">
    <div id="demo">
        <img src="css/images/tvb_logo.gif" width="20" height="20" />
    </div>
</div>

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • 老生常谈,JavaScript闭包中的this对象

    老生常谈,JavaScript闭包中的this对象

    2016-02-26 10:21

  • 学习JavaScript之this,call,apply

    学习JavaScript之this,call,apply

    2016-01-28 20:45

  • JavaScript复习笔记--字符串

    JavaScript复习笔记--字符串

    2016-01-27 17:16

  • WEB前端教程-JavaScript里的类和继承

    WEB前端教程-JavaScript里的类和继承

    2016-01-21 15:28

网友点评