Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用jQuery记录函数的执行,而不是在同一个DIV上再次运行它_Javascript_Jquery_Twitter Bootstrap - Fatal编程技术网

Javascript 如何使用jQuery记录函数的执行,而不是在同一个DIV上再次运行它

Javascript 如何使用jQuery记录函数的执行,而不是在同一个DIV上再次运行它,javascript,jquery,twitter-bootstrap,Javascript,Jquery,Twitter Bootstrap,在我的几个旋转木马幻灯片中,我将DIV的像素值转换为百分比 $('#carousel-lesson-slide').on('slid.bs.carousel', function () { // convert to percentage function convert_to_percentage(el){ var parent = el.parent(); el.css({ left:parseInt(el.css('left'))/parent.widt

在我的几个旋转木马幻灯片中,我将DIV的像素值转换为百分比

$('#carousel-lesson-slide').on('slid.bs.carousel', function () {

//  convert to 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))
    });
  }

});
因此,我的问题是:在幻灯片之间切换时,如何避免将
.drop图标夹
.drop标签夹
宽度反复转换为百分比


请注意:
.drop icon holder
.drop label holder
将出现在多张幻灯片中,但一旦将它们转换为像素百分比值,则不应再次进行转换。我需要将每一行代码保持在
slided.bs.carousel
内,因为要获得
.dropped图标保持架
dropped标签保持架
父级宽度。

在最初调用函数时,使用
data()
为每个元素存储一个标志。检查该标志,仅当其为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);
}

这是工作,正如预期:)谢谢你。现在这张罚单的标题没有多大意义,需要将标题更改为其他内容:)只是想知道是否可以使用jQuery以百分比或像素设置DIV宽度?@Syed不,不可靠。我得到了答案