2016-07-16 24 views
2

我在搜索了很多关于这篇文章标题的指南或问题之后。但我没有找到任何好的答案,所以我决定创建一个我自己的并分享结果。我仍然需要一些指导来使这个效果更好。悬停,影响其他元素并显示背景图片

请注意,我是新来的Stackoverflow和新的jQuery。

我想对我的投资组合链接有一个很好的效果。我想在链接悬停时实现的效果如下:

  1. 您悬停的链接应改变颜色。
  2. div中的所有其他链接都应该减少不透明度,以增加您悬停的链接的焦点。
  3. 当悬停在链接上时,背景图像将淡入淡出。

例子:

我已经创建了一个的jsfiddle在那里你可以本身的结果。

Jsfiddle

问题:

如果你将鼠标悬停在链接几次jQuery的功能发挥出来。我需要停止脚本 - 我该怎么做?

有没有办法将此代码写得更聪明/更好地提高网站的性能?还是我在正确的轨道上?

这里是jQuery代码:

// When hovering on class .nr-1, #section-1 will fadeIn children div .bg-1 - and so on. 

$(".nr-1").hover(function() { 
    $("#section-1").children(".bg-1").fadeIn(500); 
}, function() { 
    $("#section-1").children(".bg-1").fadeOut(500); 
}); 

$(".nr-2").hover(function() { 
    $("#section-1").children(".bg-2").fadeIn(500); 
}, function() { 
    $("#section-1").children(".bg-2").fadeOut(500); 
}); 

// When hovering a link in all the <a> tags will get the class "bla" 

$(function() { 
    $('.hover-link .nav-1 a').on('mouseenter mouseleave', function() { 
    $('.hover-link .nav-1 a').toggleClass('bla'); 
    }); 
}); 

// The link you hover over will gett a class new. 

$('.hover-link .nav-1 a').hover(function() { 
    $(this).addClass("new"); 
    }, 
    function() { 
    $(this).removeClass('new'); 
    }); 

有一个伟大的一天!

UPDATE

感谢您对所有的答案。让效果好用的最佳方法是@Redet Getachew答案。

这里是我更新的版本我Codepen!

Codepen

+0

_“如果你将鼠标悬停在链接几次jQuery的功能发挥出来。我需要停止脚本” _哪个部分的动画应该停止? '.fadeOut()','.fadeIn()'? – guest271314

+0

我认为第一个函数需要停止 - '.fadeOut()'和'.fadeIn()'。如果您将链接快速悬停几次,您会看到图像会随着您的徘徊而多次切换。 – hakanfilip

回答

1

您可以使用CSS3过渡,而不是jQuery的褪色的方法。将图像放置在链接旁边并使用脉冲(+)css选择器来影响其属性。 这可能会提高性能。请参阅以下链接。 How to affect other elements when a div is hovered

/* remove this one 
 
$(".nr-1").hover(function() { 
 
    $("#section-1").children(".bg-1").fadeIn(500); 
 
}, function() { 
 
    $("#section-1").children(".bg-1").fadeOut(500); 
 
}); 
 

 
$(".nr-2").hover(function() { 
 
    $("#section-1").children(".bg-2").fadeIn(500); 
 
}, function() { 
 
    $("#section-1").children(".bg-2").fadeOut(500); 
 
}); 
 
*/ 
 
// All the other links in the div should reduce opacity. 
 

 
$(function() { 
 
    $('.hover-link .nav-1 a').on('mouseenter mouseleave', function() { 
 
    $('.hover-link .nav-1 a').toggleClass('bla'); 
 
    }); 
 
}); 
 

 
// Effect: The link you hover, changes color. 
 

 
$('.hover-link .nav-1 a').hover(function() { 
 
    $(this).addClass("new"); 
 
    }, 
 
    function() { 
 
    $(this).removeClass('new'); 
 
    });
@import url(https://fonts.googleapis.com/css?family=Droid+Serif:400,700); 
 
/*affect .bg-1 when hovered over .nr-1 */ 
 
.nr-1:hover + .bg-1,.nr-2:hover + .bg-2{ 
 
    opacity:8.0; 
 
} 
 

 
/* General */ 
 
body { 
 
    width: 100%; 
 
    height: 100vh; 
 
    overflow: hidden; 
 
} 
 

 
.flexboxcenter { 
 
    display: flex; 
 
    justify-content: center; 
 
    align-items: center; 
 
} 
 

 
.section-1 { 
 
    width: 100%; 
 
    height: 100vh; 
 
    display: block; 
 
    position: relative; 
 
} 
 

 
.hover-link { 
 
    height: 500px; 
 
    width: 100%; 
 
    z-index: 100000; 
 
} 
 

 
.hover-link .nav-1 { 
 
    z-index: 10000; 
 
} 
 

 
.hover-link .nav-1 a { 
 
    text-align: center; 
 
    display: block; 
 
    font-family: 'Droid Serif', serif; 
 
    text-decoration: none; 
 
    color: black; 
 
    font-size: 50px; 
 
    letter-spacing: 1px; 
 
    padding: 10px; 
 
    transition: all 500ms ease-in-out; 
 
} 
 

 
/* Background classes */ 
 

 
.bg-1 { 
 
    position: absolute; 
 
    top: 0px; 
 
    left: 0px; 
 
\t background: url('https://images.unsplash.com/photo-1432821596592-e2c18b78144f?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&s=3f9c78df0edb464244bbabb04d1797d8') center center no-repeat; 
 
    background-size: cover; 
 
\t height: 100vh; 
 
\t width: 100%; 
 
\t z-index: -1; 
 
    opacity: 0.0; 
 
    -webkit-transition-property:opacity; 
 
    transition-property: opacity; 
 
    -webkit-transition-duration: 0.5s; 
 
    transition-duration: 0.5s; 
 
    -webkit-transition-timing-function: ease-out; 
 
    transition-timing-function: ease-out; 
 

 
} 
 

 
.bg-2 { 
 
    position: absolute; 
 
    top: 0px; 
 
    left: 0px; 
 
    background: url('https://images.unsplash.com/photo-1421757295538-9c80958e75b0?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&s=3628f27cd5768ece147877e2dd792c6c') center center no-repeat; 
 
    background-size: cover; 
 
\t height: 100vh; 
 
\t width: 100%; 
 
\t z-index: -1; 
 
\t opacity: 0.0; 
 
    -webkit-transition-property:opacity; 
 
    transition-property: opacity; 
 
    -webkit-transition-duration: 0.5s; 
 
    transition-duration: 0.5s; 
 
    -webkit-transition-timing-function: ease-out; 
 
    transition-timing-function: ease-out; 
 
} 
 

 
/* Hover effect classes */ 
 

 
.new { 
 
    color: #EE6F60 !important; 
 
    opacity: 1 !important; 
 
} 
 

 
.bla { 
 
    opacity: 0.3; 
 
} 
 

 
/* Hover Effect Underline From Center by Ian Lunn */ 
 

 
.hvr-underline-from-center { 
 
    display: inline-block; 
 
    vertical-align: middle; 
 
    -webkit-transform: translateZ(0); 
 
    transform: translateZ(0); 
 
    -webkit-backface-visibility: hidden; 
 
    backface-visibility: hidden; 
 
    -moz-osx-font-smoothing: grayscale; 
 
    position: relative; 
 
    overflow: hidden; 
 
} 
 
.hvr-underline-from-center:before { 
 
    content: ""; 
 
    position: absolute; 
 
    z-index: -1; 
 
    left: 50%; 
 
    right: 50%; 
 
    bottom: 0; 
 
    height: 10px; 
 
    -webkit-transition-property: left, right; 
 
    transition-property: left, right; 
 
    -webkit-transition-duration: 0.5s; 
 
    transition-duration: 0.5s; 
 
    -webkit-transition-timing-function: ease-out; 
 
    transition-timing-function: ease-out; 
 
} 
 
.hvr-underline-from-center:hover:before, .hvr-underline-from-center:focus:before, .hvr-underline-from-center:active:before { 
 
    left: 0; 
 
    right: 0; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
 
<body> 
 

 
<section id="section-1"> 
 
    
 
    <div class="hover-link flexboxcenter"> 
 
    <div class="nav-1"> 
 
     <a href="#" class="hvr-underline-from-center nr-1">Old Desk</a> 
 
     <div class="bg-1"></div> 
 
     <a href="#" class="hvr-underline-from-center nr-2">Modern Desk</a> 
 
     <div class="bg-2"></div> 
 
    </div> 
 
    </div> 
 
    
 
    
 

 
    
 
</section> 
 
    
 
</body>

+0

太棒了!这会提高网站的性能吗? – hakanfilip

+1

它的作品非常流畅。这是要走的路。谢谢! – hakanfilip

2

您可以使用.stop()

$(".nr-1").hover(function() { 
    $("#section-1").children(".bg-1").stop(true, true).fadeIn(500); 
}, function() { 
    $("#section-1").children(".bg-1").stop(true, true).fadeOut(500); 
}); 

$(".nr-2").hover(function() { 
    $("#section-1").children(".bg-2").stop(true, true).fadeIn(500); 
}, function() { 
    $("#section-1").children(".bg-2").stop(true,true).fadeOut(500); 
}); 

的jsfiddle http://jsfiddle.net/9xrgqdk7/1/

+0

谢谢!这很好! – hakanfilip