2016-07-16 102 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