简体   繁体   English

jQuery鼠标悬停动画效果

[英]jQuery mouse hover animated effect

I had to do on a WordPress website an mouse hover animated effect on the navigation menu elements. 我必须在WordPress网站上对导航菜单元素进行鼠标悬停动画效果。 I've managed to finished the job using the following code: 我设法使用以下代码完成了这项工作:

<!-- Marius was here :) -->
<style type="text/css">
.fancy-img {
    height: 51px;
    background-image: url(http://www.minortest2.dk/wp-content/themes/bizway-childtheme/images/knap.png);
    background-repeat: no-repeat;
}
.fancy-container {
    padding-top: 10px;
}
.fancy {
    position: absolute !important;
    top: 0px;
    z-index: -1;
    overflow: hidden;
}
.container_24 .grid_sub_19 {
    width: 83% !important;
}
</style>
<script type="text/javascript">
jQuery('#menu-topmenu').append('<li class="fancy"><div class="fancy-container"><div class="fancy-img"></div></div></li>');
jQuery('.menu-item-11').hover(function() {
    jQuery('.fancy').stop();
    jQuery('.fancy').css('width','107px');
    jQuery('.fancy-img').css('background-size','107px 33px');
    jQuery('.fancy').animate({ "left": "0px" }, 'fast' );
    jQuery(this).children().css('color','#1c82e0');
    jQuery(this).siblings().children().css('color','#fff');
})
jQuery('.menu-item-20').hover(function() {
    jQuery('.fancy').stop();
    jQuery('.fancy').css('width','141px');
    jQuery('.fancy-img').css('background-size','141px 33px');
    jQuery('.fancy').animate({ "left": "107px" }, 'fast' );
    jQuery(this).children().css('color','#1c82e0');
    jQuery(this).siblings().children().css('color','#fff');
})
jQuery('.menu-item-36').hover(function() {
    jQuery('.fancy').stop();
    jQuery('.fancy').css('width','155px');
    jQuery('.fancy-img').css('background-size','155px 33px');
    jQuery('.fancy').animate({ "left": "248px" }, 'fast' );
    jQuery(this).children().css('color','#1c82e0');
    jQuery(this).siblings().children().css('color','#fff');
})
jQuery('.menu-item-37').hover(function() {
    jQuery('.fancy').stop();
    jQuery('.fancy').css('width','107px');
    jQuery('.fancy-img').css('background-size','107px 33px');
    jQuery('.fancy').animate({ "left": "403px" }, 'fast' );
    jQuery(this).children().css('color','#1c82e0');
    jQuery(this).siblings().children().css('color','#fff');
})
jQuery('.menu-item-38').hover(function() {
    jQuery('.fancy').stop();
    jQuery('.fancy').css('width','111px');
    jQuery('.fancy-img').css('background-size','111px 33px');
    jQuery('.fancy').animate({ "left": "514px" }, 'fast' );
    jQuery(this).children().css('color','#1c82e0');
    jQuery(this).siblings().children().css('color','#fff');
})
</script>
<!-- Until here :D -->

The result could be seen here . 结果可以在这里看到。 Even though everything is working like I was expecting I am not satisfied with the code. 即使一切正常,如我所期望的那样,我对代码也不满意。 The positioning values are manually set like you can see above. 如上面所示,手动设置了定位值。 This could creates problem if a new element is added to the menu. 如果将新元素添加到菜单,则可能会产生问题。 Also I have some duplicate code which I want to get rid of. 我也有一些重复的代码,我想摆脱。 The menu items have common classes being a WordPress website like menu-item , menu-item-type-post_type , menu-item-object-page . 菜单项具有共同的类,这些类是WordPress网站,例如menu-itemmenu-item-type-post_typemenu-item-object-page Which is the best approach to optimize my code? 哪种是优化我的代码的最佳方法? Any guidance or help is more than welcomed. 任何指导或帮助都非常受欢迎。 I am sorry for my not so best practices code but I am still on the process of learning some of the basics. 对于我的最佳实践代码不好,我感到很抱歉,但是我仍在学习一些基础知识。 :) :)

I guess you could use $(this) selector to access information like width( $(this).width() ) of the hovered element and its relative position to the parent $(this).position().left 我猜你可以使用$(this)选择器来访问信息,例如悬停元素的width( $(this).width() )及其相对于父元素$(this).position().left相对位置

$('.menu-item').hover(function(){
$('.fancy').stop();
$('.fancy').css('width',$(this).width()+'px');
$('.fancy-img').css('background-size',$(this).width()+'px 33px');
$('.fancy').animate({ "left": $(this).position().left }, 'fast' );
});

This code may contain flaws, its just for inspiration:] 该代码可能包含缺陷,仅供参考:]

You could start with putting the code you repeat in a function and call that function with the needed parameters i guess. 您可以先将重复的代码放入函数中,然后用所需的参数调用该函数。 Look at the DRY principle if you want to learn how to write clean code. 如果您想学习如何编写干净的代码,请看一下DRY原理。 Also you could start using $ instead of jQuery. 您也可以开始使用$而不是jQuery。 That looks a lot cleaner in my opinion. 在我看来,这看起来要干净得多。

You could get the HTML element one time and put it in an variable. 您可以一次获得HTML元素并将其放入变量中。 I guess this would also save some rendering time. 我想这也可以节省一些渲染时间。

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

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