javascript - How to make hovering a button changes background of a different element -
what want happen when hovers 1 of 3 buttons "signature events" "weddings" "le coq d'or" background , content of div above change. div above static, it's not slider. styled html block.
i've started getting js within last week or so, , seems done js switch, i'm not sure. don't know search info on this.
thank you!
you can use data attribute target class , little bit jquery code.
$('button').hover( function() { $('header').addclass($(this).attr("data-class")).text($(this).attr("data-title")); }, function() { $('header').removeclass($(this).attr("data-class")).text($('header').attr("data-title")); } );
header{ background-image: url('http://lorempixel.com/200/600/'); background-size: cover; height: 180px; width: 500px; display: flex; justify-content: center; align-items: center; } header.backgroundone{ background-image: url('http://lorempixel.com/200/600/'); } header.backgroundtwo{ background-image: url('http://lorempixel.com/200/600/'); } header.backgroundthree{ background-image: url('http://lorempixel.com/200/600/'); }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <header data-title="header image"> header image </header> <button data-class="backgroundone" data-title="title one"> change header 1 </button> <button data-class="backgroundtwo" data-title="title two"> change header 2 </button> <button data-class="backgroundthree" data-title="title three"> change header 3 </button>
so if hover button, data-class attribute value button added class header element. if stop hovering, class removed again default image shown.
Comments
Post a Comment