javascript - How to record a function is executed and not to run it again over the same DIV using jQuery -
in few carousel slides converting div's pixel value percentage.
$('#carousel-lesson-slide').on('slid.bs.carousel', function () { // convert percentage function convert_to_percentage(el){ var parent = el.parent(); el.css({ left:parseint(el.css('left'))/parent.width()*100+"%", top: parseint(el.css('top'))/parent.height()*100+"%", width: el.width()/parent.width()*100+"%", height: el.height()/parent.height()*100+"%" }); } if ($('.droppable-icon-on-img-wrap').is(":visible")) { $(".dropped-icon-holder").each(function(){ convert_to_percentage($(this)) }); } if ($('.droppable-label-on-img-wrap').is(":visible")) { $(".dropped-label-holder").each(function(){ convert_to_percentage($(this)) }); } }); so, question is: how can avoid converting .dropped-icon-holder , .dropped-label-holder width percentage again , again while switching between slides.
please note: .dropped-icon-holder , .dropped-label-holder come in more 1 slide once converted pixel percentage value again conversion should not happen. need keep every line of code within slid.bs.carousel because .dropped-icon-holder , .dropped-label-holder parent's width.
store flag each element, using data(), when function called it. check flag , continue function if false:
function convert_to_percentage(el){ if(el.data('converted')) return; var parent = el.parent(); el.css({ left:parseint(el.css('left'))/parent.width()*100+"%", top: parseint(el.css('top'))/parent.height()*100+"%", width: el.width()/parent.width()*100+"%", height: el.height()/parent.height()*100+"%" }).data('converted', true); }
Comments
Post a Comment