jQuery.rotate.11.js是一個用于實現旋轉效果的jQuery插件。通過該插件,我們可以很方便地實現對網頁元素的旋轉、縮放、移動等變換效果。jQuery.rotate.11.js兼容各大主流瀏覽器,使用簡便,而且源碼清晰易懂。
$.fn.rotate = function(options) { var settings = { angle: 0, animateTo: 0, easing: function (x, t, b, c, d) { return c * (t / d) * x + b; }, duration: 1000, callback: function () { } }; if (options) { $.extend(settings, options); } var radians = Math.PI * settings.angle / 180; var cos = Math.cos(radians); var sin = Math.sin(radians); return this.each(function() { var $elem = $(this); if ($.support.opacity) { $elem.css({ '-moz-transform': 'rotate(' + settings.angle + 'deg)', '-webkit-transform': 'rotate(' + settings.angle + 'deg)', '-o-transform': 'rotate(' + settings.angle + 'deg)', '-ms-transform': 'rotate(' + settings.angle + 'deg)', 'transform': 'rotate(' + settings.angle + 'deg)' }); } else { var matrix = [cos, -sin, sin, cos, 0, 0]; var M11 = matrix[0]; var M12 = matrix[1]; var M21 = matrix[2]; var M22 = matrix[3]; filterstring = "(progid:DXImageTransform.Microsoft.Matrix(M11=" + M11 + ", M12=" + M12 + ", M21=" + M21 + ", M22=" + M22 + ", sizingMethod='auto expand'))"; var margin_top = $elem.height() / 2; var margin_left = $elem.width() / 2; $elem.css({ 'margin-top': -margin_top + 'px', 'margin-left': -margin_left + 'px', 'filter': filterstring }); } $elem.animate({ rotate: settings.animateTo }, { duration: settings.duration, easing: settings.easing, complete: settings.callback, step: function (now) { if ($.support.opacity) { $elem.css({ '-moz-transform': 'rotate(' + now + 'deg)', '-webkit-transform': 'rotate(' + now + 'deg)', '-o-transform': 'rotate(' + now + 'deg)', '-ms-transform': 'rotate(' + now + 'deg)', 'transform': 'rotate(' + now + 'deg)' }); } else { var radians = Math.PI * (now) / 180; var cos = Math.cos(radians); var sin = Math.sin(radians); $elem.css({ 'filter': "\"progid:DXImageTransform.Microsoft.Matrix(M11=" + cos + ", M12=" + (-sin) + ", M21=" + sin + ", M22=" + cos + ", sizingMethod='auto expand')\"" }); } } }); }); };
通過對該插件的理解和使用,我們可以極大地提高網頁的用戶交互性,同時也為網站添加了更多效果呈現的手段。值得一提的是,jQuery.rotate.11.js是一個開源的插件,我們可以前往Github等開源代碼托管平臺獲取插件的最新版本。