jquery - Make a .click override .on('mouseover') -
im making timeline, , shows info when being hovered over, want clickable, , when clicked, stay visible until button clicked, or button clicked again. how do this?
my html:
<div class="timeline-pills"> <div class='timeline-element' id='year-1492'> <p>1492</p> </div> <div class='timeline-element' id='year-1607'> <p>1607</p> </div> <div class='timeline-element' id='year-1620'> <p>1620</p> </div> </div> <div class="timeline-info-panels"> <div class='timeline-info' id='1492'> <p>1492</p> </div> <div class='timeline-info' id='1607'> <p>1607</p> </div> <div class='timeline-info' id='1620'> <p>1620</p> </div> </div>
my jquery:
$('.timeline-element').on('mouseenter', function(){ $(this).addclass('hover-over-time-pill') $('.timeline-info-panels').show(); var hoverid = $(this).attr("id"); var newid = hoverid.replace('year-', ''); $('#'+newid).show(); }); $('.timeline-element').on('mouseleave', function(){ $(this).removeclass('hover-over-time-pill') $('.timeline-info-panels').hide(); var hoverid = $(this).attr("id"); var newid = hoverid.replace('year-', ''); $('#'+newid).hide(); }); $('.timeline-element').click(function(){ $(this).addclass('timeline-click'); $('.timeline-info-panels').show(); var clickid = $(this).id; var newid = clickid.substring(0,5); $('#'+newid).show(); }); $('.timeline-element').click(function(){ $(this).removeclass('timeline-click'); $('.timeline-info-panels').hide(); var clickid = $(this).id; var newid = clickid.substring(0,5); $('#'+nweid).hide(); });
you set active timelines on click event.
first add active class pill , info on click.
$('.timeline-element').click(function () { // removes old active elements second click $('.timeline-element.active,.timeline-info.active').removeclass('active'); var hoverid = $(this).attr("id"); var newid = hoverid.replace('year-', ''); // set active $('#' + newid).addclass('active'); $(this).addclass('active'); });
then go css , write new style active class.
.timeline-info.active { display:block !important; } .timeline-element.active{ background-color: #051a5b; }
http://jsfiddle.net/s4yvbqzl/3/ there working demo.
edit close activated elements
$('.timeline-element').click(function () { var deactive = $(this).hasclass('active'); // removes old active elements second click $('.timeline-element.active,.timeline-info.active').removeclass('active'); // if clicked element not active, activate it. if(deactive == false) { var hoverid = $(this).attr("id"); var newid = hoverid.replace('year-', ''); // set active $('#' + newid).addclass('active'); $(this).addclass('active'); } });
working demo http://jsfiddle.net/s4yvbqzl/4/
Comments
Post a Comment