简体   繁体   English

jQuery动画从左到右的宽度

[英]Jquery animate width from left to right

I am trying to animate menu elements shrinking the width from 170 to 150px on mouseover. 我试图制作动画菜单元素,将鼠标悬停时的宽度从170像素缩小到150像素。 My problem is, with the default .animate shrinking happens on the right side of my rectangle, I need it to shrink on the left side. 我的问题是,默认.animate收缩发生在矩形的右侧,因此我需要在左侧收缩。 I have already tried animating left margin, but since I have text inside the element, it shifts on the right during the animation. 我已经尝试过动画左边界,但是由于元素中有文本,因此在动画过程中它会向右移动。

This is the actual code I'm using 这是我正在使用的实际代码

$(document).ready(function(){  

//When mouse rolls over  
$("#navigation li").mouseover(function(){  
    $(this).stop().animate({marginLeft:'20'},{queue:false, duration:600, easing: 'easeOutBounce'}) 

});  

//When mouse is removed  
$("#navigation li").mouseout(function(){  
    $(this).stop().animate({marginLeft:'0'},{queue:false, duration:600, easing: 'easeOutBounce'})  
});  


}); 

I had found some other answers to this, but I didn't understand the proposed solution. 我找到了其他答案,但是我不理解所建议的解决方案。

This is the default behavior. 这是默认行为。 The only way to change this is via CSS 改变这一点的唯一方法是通过CSS

#navigation li {
float: right; //Sometimes using float funks things up so...
}

or changing the CSS of the parent and the child li (better solution) 或更改父级和子级li的CSS(更好的解决方案)

#navigation_li's_parent {
text-align: right;
}

#navigation li {
display: inline-block;
}

You can easily control this with css positioning. 您可以使用css定位轻松地控制它。 Either with floats or with an inner container that is positioned either left, or right (relative, or absolute). 带有浮子或位于左侧或右侧(相对或绝对)的内部容器。

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

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