简体   繁体   English

如何将元素旋转添加到jQuery?

[英]How to add element rotation to jQuery?

I'm trying to do a simple extension of jQuery to allow image rotation via $("element").rotate(arbitraryNumber); 我正在尝试做一个简单的jQuery扩展,以允许通过$("element").rotate(arbitraryNumber);进行图像旋转$("element").rotate(arbitraryNumber); later on. 稍后的。

From my understanding this is the correct method for setting the transform properties using jquery. 据我了解,这是使用jquery设置转换属性的正确方法。 I get the feeling I've forgotten something obvious.. 我感到自己忘记了一些明显的事情。

    jQuery.fn.rotate=function(deg){
                        this.css({'transform': 'rotate('+deg+'deg)'});
                        this.css({'-ms-transform': 'rotate('+deg+'deg)'});
                        this.css({'-moz-transform': 'rotate('+deg+'deg)'});
                        this.css({'-o-transform': 'rotate('+deg+'deg)'}); 
                        this.css({'-webkit-transform': 'rotate('+deg+'deg)'});
                        return this; 
                    };

EDIT: CODE REDUCTION 编辑:减少代码

jQuery.fn.rotate=
function(deg){
                $(this).css({ 'transform': 'rotate('+deg+'deg)','-ms-transform': 'rotate('+deg+'deg)', '-moz-transform': 'rotate('+deg+'deg)', 
                    '-o-transform': 'rotate('+deg+'deg)', '-webkit-transform': 'rotate('+deg+'deg)' });
                return this;
             };

FURTHER EDIT Sorry my mistake, there was just a typo further in the calling. 进一步编辑对不起,我的错,电话中还有一个错别字。

(function( $ ){
$.fn.rotate = function(deg) {
    this.css({'transform': 'rotate('+deg+'deg)'});
    this.css({'-ms-transform': 'rotate('+deg+'deg)'});
    this.css({'-moz-transform': 'rotate('+deg+'deg)'});
    this.css({'-o-transform': 'rotate('+deg+'deg)'}); 
    this.css({'-webkit-transform': 'rotate('+deg+'deg)'});
    return this; 
};
})( jQuery );

//example
$(document).ready(function(){
    $('#sample').rotate(60);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM