html - jquery toggleClass changing only once -
i making slideshow jquery. i'm using jquery ui icons play/pause button , on. have code:
$(function(){ $(".controls").hide(); //hide controls $(".slider").hover(function(){ $(".controls").fadein(500); }, function(){ $(".controls").fadeout(500); }); // show controls on hover $(".play").click(function(){ $(".slider").attr({'data-running' : 'true', 'data-paused' : 'false'}); $(this).toggleclass("ui-icon-play ui-icon-pause play pause"); }); $(".pause").click(function(){ $(".slider").attr({'data-running' : 'false', 'data-paused' : 'true'}); $(this).toggleclass("ui-icon-play ui-icon-pause play pause"); }); //add play/pause functionality });
html:
<div class="slider-wrap"> <div class="slider" data-running="false" data-paused="true"> <img class="starter-img" src="http://images.freeimages.com/images/previews/e16/tree-1533498.jpg" /> <a href="http://labs.blogs.com/.a/6a00d8341caed853ef016768796ee4970b-pi" role="img-data"></a> <a href="http://www.successconnections.com/articles/wp-content/uploads/2013/04/technology.jpg" role="img-data"></a> <span class="controls"> <span class="ui-icon ui-icon-circle-triangle-w prev control"></span> <span class="ui-icon ui-icon-play play control"></span> <span class="ui-icon ui-icon-circle-triangle-e seek-for control"> </span> </div> </div>
the first time click on play button, switches data-running
attribute true
, data-paused
attribute false
, , it's own class ui-icon-pause pause
, should. when click again, it's own class changes, data-running
, data-paused
don't change. why aren't attribute changes being made? fiddle
the $(".pause")
not bind when flip classes. finds elements there @ moment in time. either use event delegation or add logic inside 1 click handles both states.
event delegation:
$(".controls").on("click", ".play", function(){ $(".slider").attr({'data-running' : 'true', 'data-paused' : 'false'}); $(this).toggleclass("ui-icon-play ui-icon-pause play pause"); }); $(".controls").on("click", ".pause", function(){ $(".slider").attr({'data-running' : 'false', 'data-paused' : 'true'}); $(this).toggleclass("ui-icon-play ui-icon-pause play pause"); }); //add play/pause functionality
better in 1 since code 95% same
$(".controls").on("click", ".play", function(){ $(this).toggleclass("ui-icon-play ui-icon-pause play pause"); var ispaused = $(this).hasclass("pause"); $(".slider").attr({'data-running' : !ispaused, 'data-paused' : ispaused}); });
Comments
Post a Comment