【问题标题】:Increase element width by percentage using jQuery使用 jQuery 按百分比增加元素宽度
【发布时间】:2015-11-14 17:54:14
【问题描述】:

VarinderThis answer 效果很好,但我想获得以像素为单位设置的元素宽度并将其增加 10%。

$(window).load(function() {
    $(".ml").each(function() { // this is kindof sugary way of doing for loop over array of elements - which is $(".ml")
         var $this = $(this); // the current jQueried .ml element
         var currentWidth = $this.width(); // get width of current element, width is a jQuery method: https://api.jquery.com/width/
         var newWidth = currentWidth + 5; // increment the width
         $this.width(newWidth); // pass in the new width as the parameter.
    });
});

【问题讨论】:

    标签: javascript jquery width percentage


    【解决方案1】:

    我认为有几种方法可以做到这一点,试试吧;

     var currentWidth = $this.width();
    
     var newWidth = currentWidth * 1.1; //increment by 10%
     //OR
     var newWidth = currentWidth + ( currentWidth * 10) / 100; //increment by 10%
    

    希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      尝试用var newWidth = currentWidth * 1.1;替换currentWidth

      【讨论】:

        【解决方案3】:

        您只需将新宽度设置为旧宽度 + 旧宽度的 10%:

        var newWidth = currentWidth + (currentWidth * 0.1);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-05-24
          • 2019-04-28
          • 1970-01-01
          • 2012-04-22
          • 1970-01-01
          • 2013-01-22
          相关资源
          最近更新 更多