admin管理员组文章数量:1406031
I trying to animate listfrom left to right and vice versa,now i want when mouse hover on div list animate left to right and when mouse leave that div than it list animate right to left , can anyone help me for this.
$('.ul_div').mouseover(function () {
$('.ul_div').css({ display: 'block' });
$('.ul_div ul').animate({ left: '100px', background: '#ccc' }, 100);
});
<div class="ul_div">Hover Me<div>
<ul id='ulEle'>
<li><a href="">demo</a></li>
<li><a href="">demo</a></li>
<li><a href="">demo</a></li>
<li><a href="">demo</a></li>
</ul>
Demo Here
I trying to animate listfrom left to right and vice versa,now i want when mouse hover on div list animate left to right and when mouse leave that div than it list animate right to left , can anyone help me for this.
$('.ul_div').mouseover(function () {
$('.ul_div').css({ display: 'block' });
$('.ul_div ul').animate({ left: '100px', background: '#ccc' }, 100);
});
<div class="ul_div">Hover Me<div>
<ul id='ulEle'>
<li><a href="">demo</a></li>
<li><a href="">demo</a></li>
<li><a href="">demo</a></li>
<li><a href="">demo</a></li>
</ul>
Demo Here
Share Improve this question edited Sep 9, 2013 at 10:54 Rory McCrossan 338k41 gold badges320 silver badges351 bronze badges asked Sep 9, 2013 at 10:51 S. S. RawatS. S. Rawat 6,1314 gold badges44 silver badges59 bronze badges 1- 1 FYI please include all code in your question. A link to a fiddle is great, but it will be useless if jsfiddle goes offline. – Rory McCrossan Commented Sep 9, 2013 at 10:54
2 Answers
Reset to default 2jsFiddle Demo
CSS:
.ul_div ul {
position: absolute;
display: none
}
.ul_div {
border:2px solid Red;
position: relative;
}
Javascript:
$('.ul_div').hover(
function () {
$('.ul_div ul').css({
display: 'block'
});
$('.ul_div ul').animate({
left: '100px',
background: '#ccc'
}, 100);
},
function () {
$('.ul_div ul').animate({
left: '0',
background: '#ccc'
}, 100, function () {
$('.ul_div ul').css({
display: 'none'
});
});
});
You need to specify positioning on the ul
element in order to use the CSS left
and right
properties.
Update your CSS as follows:
#ulEle {
position: absolute;
}
.ul_div {
border:2px solid Red;
position: relative;
}
You should then update your jQuery code to use the hover()
function, and use the following code:
$('.ul_div').hover(function () {
$('.ul_div ul').animate({ left: '100px', background: '#ccc' }, 100);
}, function() {
$('.ul_div ul').animate({ left: '0px', background: '#ccc' }, 100);
});
Please see the updated jsFiddle for a demo.
本文标签: javascriptanimate div on mouse hover and leaveStack Overflow
版权声明:本文标题:javascript - animate div on mouse hover and leave - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744963689a2634811.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论