admin管理员组文章数量:1313599
Per the standards, I'm trying to use jQuery's on() method as much as possible for my event handlers. Instead of hover(), I tried using on('hover') but it does not work. What can I do to make this bit of code work with on() instead of hover()? Is there a list of events I can see that work with the on() method?
$(document).ready(function(){
$('#navigation li').on('hover', function(){
$(this).animate({
paddingLeft: '+=15px'
}, 200);
}, function(){
$(this).animate({
paddingLeft: '-=15px'
}, 200);
});
});
Per the standards, I'm trying to use jQuery's on() method as much as possible for my event handlers. Instead of hover(), I tried using on('hover') but it does not work. What can I do to make this bit of code work with on() instead of hover()? Is there a list of events I can see that work with the on() method?
$(document).ready(function(){
$('#navigation li').on('hover', function(){
$(this).animate({
paddingLeft: '+=15px'
}, 200);
}, function(){
$(this).animate({
paddingLeft: '-=15px'
}, 200);
});
});
Share
Improve this question
asked Jan 8, 2016 at 19:18
GTS JoeGTS Joe
4,19217 gold badges61 silver badges109 bronze badges
3
- 2 well... hover isn't an event for starters. – Kevin B Commented Jan 8, 2016 at 19:19
- As far as what events it supports, that is implemented by the browser. – Kevin B Commented Jan 8, 2016 at 19:20
- 1 Incidentally, this could easily be replaced with pure CSS transitions. – Blazemonger Commented Jan 8, 2016 at 19:31
3 Answers
Reset to default 6
$('div')
.on('mouseenter', function(){ $(this).addClass('red'); })
.on('mouseleave', function(){ $(this).removeClass('red'); });
div {
min-height: 50px;
border: 1px solid #000;
}
.red { background-color: #F00; }
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div></div>
Well hover take two functions. On takes one.
$(document).ready(function(){
$('#navigation li').on('mouseenter', function(){
$(this).animate({
paddingLeft: '+=15px'
}, 200);
});
$('#navigation li').on('mouseleave', function(){
$(this).animate({
paddingLeft: '-=15px'
}, 200);
});
});
Well, I'm late to the party but I think this should do
$(document).ready(function(){
$('#navigation li').on('mouseover', function(){
$(this).animate({
paddingLeft: '+=15px'
}, 200);
}, function(){
$(this).animate({
paddingLeft: '-=15px'
}, 200);
});
});
本文标签: javascriptUsing jQuery on() Instead of hover()Stack Overflow
版权声明:本文标题:javascript - Using jQuery on() Instead of hover() - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741951165a2406714.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论