How to create dynamic diagonal line from left-bottom to right-top corner?

前端 未结 3 844
别那么骄傲
别那么骄傲 2021-02-04 16:17

I\'ve created a simple layout where I have three divs which interact. One is the logo in the middle of the screen and the other are two blocks which with jQuery are moved out of

3条回答
  •  情话喂你
    2021-02-04 16:49

    Use trigonometry to compute the desired angle:

    var angle = Math.atan2($(window).width(),$(window).height()); // in radians
    $('#blocktop,#blockbottom').css('transform','skew(-'+angle+'rad)');
    

    (Note for math geeks and other pedants: the arctangent would normally take the height divided by the width, not the other way around. In this case, however, we're skewing a vertical line instead of a horizontal one, so the above code gives the desired result.)

    Note that newer versions of jQuery will automatically add the necessary -webkit- or -moz- prefix to that CSS transform property.

    You might also want to display:none the elements until the above code can alter the angle, and then show() them immediately after the angle is computed:

    $('#blocktop,#blockbottom').css('transform', 'skew(-' + angle + 'rad)')
        .add('#logo').show();
    

    http://jsfiddle.net/mblase75/6a93T/10/

提交回复
热议问题