2010-06-11 39 views
45

使用jQuery可以旋轉div嗎?我可以旋轉圖像,但不能旋轉div;有沒有解決方案?如何使用jQuery旋轉div div

+0

[jQuery中旋轉一個DIV元素]的可能重複(http://stackoverflow.com/questions/382591/rotating-a-div-element-in-jquery) – Bobby 2010-06-11 07:28:13

+1

@Sjoerd:請閱讀常見問題解答:http://meta.stackexchange.com/questions/8724/how-to-deal-with-google-questions – Bobby 2010-06-11 07:28:48

+3

我沒有得到那些關閉問題的規則。人們正試圖爲他們的問題尋找解決方案(而不僅僅是提出問題的人)。我發現這一點,我知道有更好的解決方案,所以我一直在尋找。但我不能回來,在這裏放置更好的答案,因爲「他沒有努力去尋找他自己的解決方案」。這個網站的重點並不是要在儘可能短的時間內提供針對ppl的答案嗎?當他不是自己嘗試自己「欺騙」自己 - 他不會成爲優秀的程序員,但他的戰鬥不是我們或你的。 – Srneczek 2015-06-23 09:58:12

回答

68

編輯:更新了jQuery的1.8

因爲jQuery的1.8瀏覽器特定的轉換將被自動添加。 jsFiddle Demo

var rotation = 0; 

jQuery.fn.rotate = function(degrees) { 
    $(this).css({'transform' : 'rotate('+ degrees +'deg)'}); 
    return $(this); 
}; 

$('.rotate').click(function() { 
    rotation += 5; 
    $(this).rotate(rotation); 
}); 

編輯:添加代碼,使其一個jQuery功能。

對於那些不想進一步閱讀的人,在這裏,你去。有關更多詳細信息和示例,請繼續閱讀。 jsFiddle Demo

var rotation = 0; 

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

$('.rotate').click(function() { 
    rotation += 5; 
    $(this).rotate(rotation); 
}); 

編輯:一個對這個職位的評論中提到jQuery Multirotation。這個jQuery插件基本上支持IE8執行上述功能。如果你想要最大的兼容性或更多的選擇,這可能是值得使用的。但爲了減少開銷,我建議上述功能。它可以用於IE9 +,Chrome,Firefox,Opera等等。


鮑比......這是誰真正想要做到這一點在JavaScript的人。這可能需要在JavaScript回調中旋轉。

這是jsFiddle

如果您想以自定義間隔進行旋轉,可以使用jQuery手動設置css而不是添加類。像this!我在答案的底部包含了兩個jQuery選項。

HTML

<div class="rotate"> 
    <h1>Rotatey text</h1> 
</div> 

CSS

/* Totally for style */ 
.rotate { 
    background: #F02311; 
    color: #FFF; 
    width: 200px; 
    height: 200px; 
    text-align: center; 
    font: normal 1em Arial; 
    position: relative; 
    top: 50px; 
    left: 50px; 
} 

/* The real code */ 
.rotated { 
    -webkit-transform: rotate(45deg); /* Chrome, Safari 3.1+ */ 
    -moz-transform: rotate(45deg); /* Firefox 3.5-15 */ 
    -ms-transform: rotate(45deg); /* IE 9 */ 
    -o-transform: rotate(45deg); /* Opera 10.50-12.00 */ 
    transform: rotate(45deg); /* Firefox 16+, IE 10+, Opera 12.10+ */ 
} 

jQuery的

確保這些被包裹在$(文件)。就緒

$('.rotate').click(function() { 
    $(this).toggleClass('rotated'); 
}); 

定製間隔

var rotation = 0; 
$('.rotate').click(function() { 
    rotation += 5; 
    $(this).css({'-webkit-transform' : 'rotate('+ rotation +'deg)', 
       '-moz-transform' : 'rotate('+ rotation +'deg)', 
       '-ms-transform' : 'rotate('+ rotation +'deg)', 
       'transform' : 'rotate('+ rotation +'deg)'}); 
}); 
+0

那輪換比較好? css或jquery,http://plugins.jquery.com/multirotation/。謝謝。 – 2013-06-28 11:10:32

+0

在關於此插件的回答中添加了信息。看看插件的源代碼。它支持IE8和其他一些功能,但除此之外,它確實完成了這項工作+開銷。 – screenmutt 2013-06-28 11:25:46

+0

簡單和完美的作品。感謝您使它成爲一個jQuery函數。你可以用'$ .fn.rotate'替換'jQuery.fn.rotate'的定義 - 做同樣的事情,但更簡潔。 – clearlight 2015-12-15 02:53:21