2016-07-16 5 views
2

私はガイドやこの記事のタイトルに似た質問の後でたくさん検索してきました。しかし、私は良い答えを見つけることができませんでしたので、私は自分のものを作り、結果を共有することに決めました。この効果をより良くするためにはまだ何か指針が必要です。他の要素を有効にして背景画像を表示する

ここで私はStackoverflowとjQueryの新機能に注目してください。

私のポートフォリオのリンクには、見た目の良い効果があります。リンク上でマウスを動かしたときに達成したい効果は、次のとおりです。

  1. リンク先の色が変わる必要があります。
  2. div内の他のすべてのリンクは、あなたがホバリングしているリンクのフォーカスを高めるために不透明度を減らす必要があります。
  3. リンク上をホバーすると、背景画像がフェードインまたはフェードインします。

例:

私はあなたが結果をSEができ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

+0

_「あなたはリンクの上に数回を置くとjQueryの関数が出て再生されます。私は、スクリプトを停止する必要がある」_アニメーションの一部を停止すべき? '.fadeOut()'、 '.fadeIn()'? – guest271314

+0

私は最初の関数が '.fadeOut()'と '.fadeIn()'を停止する必要があると思います。リンクを数回速く動かすと、あなたが持ち歩いた回数だけ画像が切り替わります。 – hakanfilip

答えて

1

を使用することができます。画像をリンクの隣に置き、パルス(+)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

あなたが代わりにjqueryのフェード方法のCSS3トランジションを使用することができます.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

関連する問題