javascript - Click on item to change background image of another -
trying use javascript create "button" of sorts using hovers on li items change background of li item (phonechange). items surround image of phone screenshots i'd change when hovering on related li item. i've been looking similar example here , haven't been able find works me.
<ol class="list-b"> <li class="firstphone"><span class="title">first box</span> lorem ipsum sit dolor.</li> <li class="secondphone"><span class="title">second box</span> lorem ipsum sit dolor.</li> <li class="thirdphone"><span class="title">third box</span> lorem ipsum sit dolor.</li> <li class="fourthphone"><span class="title">fourth box</span> lorem ipsum sit dolor.</li> <li class="fifthphone"><span class="title">fifth box</span> lorem ipsum sit dolor.</li> <li class="sixthphone"><span class="title">sixth box</span> lorem ipsum sit dolor.</li> <li id="phonechange" class="firstphone"></li> </ol>
css
.firstphone{ background: url(../images/mobile1.png) } .secondphone{ background: url(../images/mobile2.png) } .thirdphone{ background: url(../images/mobile3.png)} .fourthphone{ background: url(../images/mobile4.png)} .fifthphone{ background: url(../images/mobile5.png)} .sixthphone{ background: url(../images/mobile6.png)}
add identifying class
each "button" (i called button sake of simplicity):
<ol class="list-b"> <li class="button firstphone"><span class="title">first box</span> lorem ipsum sit dolor.</li> <li class="button secondphone"><span class="title">second box</span> lorem ipsum sit dolor.</li> <li class="button thirdphone"><span class="title">third box</span> lorem ipsum sit dolor.</li> <li class="button fourthphone"><span class="title">fourth box</span> lorem ipsum sit dolor.</li> <li class="button fifthphone"><span class="title">fifth box</span> lorem ipsum sit dolor.</li> <li class="button sixthphone"><span class="title">sixth box</span> lorem ipsum sit dolor.</li> <li id="phonechange"></li> </ol>
attach event handler elements:
var buttons = document.getelementsbyclassname('button'); (var = 0; < buttons.length; i++) { buttons[i].addeventlistener('click', function() { document.getelementbyid('phonechange').css.background = this.css.background; }); }
Comments
Post a Comment