使用 jQuery UI 实现动画效果 addClass/removeClass
为了向 addClass/removeClass 添加淡入淡出过渡,我使用 jQuery UI:
$(document).ready(function(){
$('header nav a').hover(
function(){
$('header').stop().removeAttr("style").addClass('header-pink', 'slow')
},
function(){
$('header').stop().removeClass('header-pink', 'slow')
}
)
});
我的目标是:当 :hover 子元素(悬停)时更改(使用淡入淡出动画/效果)父元素(标题)的背景颜色导航a)。 css 类非常简单:
.header-pink { background: pink; }
HTML:
<header>
<nav>
<ul>
<li><a href="">Link 1</a></li>
<li><a href="">Link 2</a></li>
<li><a href="">Link 3</a></li>
</ul>
</nav>
</header>
当然,我调用 jQuery 和 jQuery UI 时没有错误:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js"</script>
<script src="js/libs/jquery.effects.core.min.js"></script>
jQuery 脚本无法正常工作,因为:
- 该类已添加(onmouseover),但没有过渡(淡入淡出)效果。
- 该类以不需要的方式添加:
我使用
.removeAtrr("style")
来避免这种奇怪的行为。 - 该类不会被删除 (onmouseout)。
使用 .animate()
我更愿意避免使用 jQuery UI 来设置颜色动画,因为我使用 LESS 的变量,当然,我只想修改样式 请检查我的项目以获取想法,如
您所见,我的目的是。当您将光标放在链接上时,将固定
的背景颜色替换为链接的颜色,换句话说,将 < 的背景颜色替换。 /code> 以过渡淡入淡出的方式转动一旦指针悬停在链接“Posts”或“Links”上,动画/效果就会变为蓝色或粉红色,但它会恢复其原始状态(没有 css 类,.removeClass()
)一旦指针移出,就会具有相同的过渡淡入淡出动画/效果。请注意,我的项目仍在开发中,我目前正在测试所有链接具有相同属性的效果;当我实现所需的结果时,我将为每个链接应用单独的属性(背景颜色)。
In order to add a fade transition to addClass/removeClass I am using jQuery UI:
$(document).ready(function(){
$('header nav a').hover(
function(){
$('header').stop().removeAttr("style").addClass('header-pink', 'slow')
},
function(){
$('header').stop().removeClass('header-pink', 'slow')
}
)
});
My goal is: to change (with a fade animation/effect) the background-color of the parent element (header) when :hover the child element (hover nav a). The css class is very simple:
.header-pink { background: pink; }
The HTML:
<header>
<nav>
<ul>
<li><a href="">Link 1</a></li>
<li><a href="">Link 2</a></li>
<li><a href="">Link 3</a></li>
</ul>
</nav>
</header>
Of course, I am calling jQuery and jQuery UI with no errors:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js"</script>
<script src="js/libs/jquery.effects.core.min.js"></script>
The jQuery script does not work properly because:
- The class is added (onmouseover) but with no transition (fade) effect.
- The class is added in a undesired way:
<header style="background:
I use
pink;">.removeAtrr("style")
to avoid this odd behavior. - The class is not removed (onmouseout).
Using .animate()
to animate colors with jQuery UI is something I prefer to avoid because I am using variables with LESS and, of course, I want the style only to be modified by the stylesheet.
Please, check my project to get an idea. As you can see, my intention is to replace the background color of the fixed <header>
with the color of the links when you place the cursor over the links. In other words, the background color of the <header>
turns with a transitional fade animation/effect as blue or pink as soon as the pointer is :hover the link "Posts." or "Links." but it recovers its original state (with no css class, .removeClass()
) as soon as the pointer is out with the same transitional fade animation/effect. Please note that my project is still in development and I am currently testing the effect with same properties for all the links; when I accomplish the desired result I will apply the individual properties (background-color) for every link.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论