Javascript - Changing border-width without moving surrounding elements.

前端 未结 8 1395
萌比男神i
萌比男神i 2021-02-14 09:43

I have the following HTML:

Hover:


相关标签:
8条回答
  • 2021-02-14 10:11

    When I do this and I want that same +border -movement result, I like to do this:

    function() { 
        $(this).css('borderWidth', '7px');
        $(this).css('margin', '-7px');
    });
    

    The negative margin brings everything back in.

    You can also condense the styles into an object and pass them in one .css() call:

    $(this).css( {borderWidth: '7px', margin: '-7px'} );
    
    0 讨论(0)
  • 2021-02-14 10:16

    You could add to the padding if you subtract from the border and vice versa.

    0 讨论(0)
  • 2021-02-14 10:16

    decrease the margin with the same amount of pixels you increase the border width with

    0 讨论(0)
  • 2021-02-14 10:16

    Decrease the width by two times the border increase ..

     $('.Size').hover(
      function() {
          var width = parseInt($(this).css('width'));
          $(this).css('borderWidth', '5px').css({width:width-8});
      },
      function() {
          var width = parseInt($(this).css('width'));
        $(this).css('borderWidth', '1px').css({width:width+8});
      })
    

    have a look at http://jsfiddle.net/rBrZL/

    0 讨论(0)
  • 2021-02-14 10:17

    You can use "outline" instead of border is browser support is OK with you.

    0 讨论(0)
  • 2021-02-14 10:30

    Yes, add a position: relative; left: -5px; to position it correctly.

    0 讨论(0)
提交回复
热议问题