简体   繁体   English

在jQuery中从右到左的动画宽度切换

[英]Animate width toggle from right to left in jQuery

I want to slide a hidden ( display:none ) div block from right to left using jQuery. 我想使用jQuery从右向左滑动一个隐藏的( display:none )div块。 It should appear when a user hovers on this block. 当用户将鼠标悬停在此块上时,它应该出现。 Here's code: 这是代码:

$('.wrapper').hover(function () {
    $(this).children('.item').stop(true, true).animate({width:'toggle'}, 400);
});

This code works fine, but it slides from left to right, and I want it to slide from right to left. 这段代码可以正常工作,但是它从左向右滑动,我希望它从右向左滑动。 How can I 'reverse' it? 我如何“逆转”它?

The animation makes width of the element change and when it is positioned in a usual manner, it gets shrinked to the left side (think of it this way: where will your block end up if it get 50% width ?). 动画使元素的width发生变化,以通常的方式放置时,它会缩小到左侧(以这种方式思考:如果块获得50%的width ,它将在哪里结束?)。 You can position the div in another way, say with float: right; 您可以用另一种方式定位div ,用float: right; and in this case it will be shrinked to the right side. 在这种情况下,它将缩小到右侧。

Here's a snippet that illustrates this: 这是说明这一点的代码段:

 window.toggle = function() { jQuery(".toggledOne, .toggledAnother").stop(true, true).animate({width:'toggle'}, 400); } 
 .wrapper, .toggledOne, .toggledAnother { width: 100%; } .wrapper { height: 2em; } .toggledOne, .toggledAnother { height: 1em; } .toggledOne { background-color: red; } .toggledAnother { float: right; background-color: green; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <a onclick="window.toggle();">click me</a> <div class="wrapper"> <div class="toggledOne"></div> <div class="toggledAnother"></div> </div> 

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

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