How to increase/decrease current margin at a number by jquery?

后端 未结 4 1852
慢半拍i
慢半拍i 2020-12-17 16:58

Given an element with unknown margin-left, how to increase its margin-left at a number say 100px?

For example:

assuming the original margin-left is 100px

4条回答
  •  南笙
    南笙 (楼主)
    2020-12-17 17:23

    $('#element-id').css('margin-left', function (index, curValue) {
        return parseInt(curValue, 10) + 100 + 'px';
    });
    

    curValue will include 'px' at the end, so you'll end up with NaN or 100px100px as your result without using parseInt first.

提交回复
热议问题