简体   繁体   English

如何从左下角到右上角创建动态对角线?

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

I've created a simple layout where I have three divs which interact. 我创建了一个简单的布局,其中有三个div相互作用。 One is the logo in the middle of the screen and the other are two blocks which with jQuery are moved out of the screen. 一个是屏幕中间的徽标,另一个是两个块,jQuery被移出屏幕。 I used the skew option from CSS to apply a degree transformation. 我使用CSS的skew选项来应用学位转换。 I would like to apply the certain degree depending on the screen, so this degree will apply to all screens correctly. 我想根据屏幕应用一定程度,所以这个学位将适用于所有屏幕。

Visual example: http://jsfiddle.net/6a93T/1/ 可视化示例: http//jsfiddle.net/6a93T/1/

For now I have this code: 现在我有这个代码:

HTML: HTML:

<html>
    <header>
        <link rel="stylesheet" type="text/css" href="css/reset.css">
        <link rel="stylesheet" type="text/css" href="css/style.css">
        <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
        <script type="text/javascript" src="js/jq.animation.js"></script>
    </header>
    <body>
        <div id="preloader">
            <div id="blocktop"></div>
            <div id="logo"></div>
            <div id="loadline"></div>
            <div id="blockbottom"></div>
        </div>
    </body>
</html>

CSS: CSS:

html{
    overflow: hidden;
}


#preloader{
    width: 100%;
    height: 100%;
}

#logo{
    background-image: url('../img/logotest.png');
    width: 300px;
    height: 300px;
    display: block;
    position: fixed;
    top: 50%;
    left: 50%;
    margin-left: -150px;
    margin-top: -150px;
    z-index: 1000;
}


#blocktop{
    background-color: #fff4ed;
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0px;
    left: -50%;
    z-index: 10;

    transform: skew(-45deg);
     -o-transform: skew(-45deg);
     -moz-transform: skew(-45deg);
     -webkit-transform: skew(-45deg);
}

#blockbottom{
    background-color: #ff7f33;
    width: 100%;
    height: 100%;
    position: absolute;
    bottom: 0px;
    right: -50%;

    transform: skew(-45deg);
     -o-transform: skew(-45deg);
     -moz-transform: skew(-45deg);
     -webkit-transform: skew(-45deg);

}

jQuery: jQuery的:

$(document).ready(function(){

    /*$("button").click(function() */
        setTimeout(function(){

        $("#blocktop").animate({
        left: '-120%',
        opacity: '0'},
        800
      );

        $("#blockbottom").animate({
        right: '-120%',
        opacity: '0'},
        800
      );


        $('#logo').fadeOut('700')
    },2000);

}); 

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. 请注意,较新版本的jQuery会自动将必要的-webkit--moz-前缀添加到该CSS transform属性中。

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: 您可能还希望display:none元素,直到上面的代码可以改变角度,然后在计算角度后立即show()它们:

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

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

I just use the fact that a DOM-Element with two different border for top and right results in a diagonal line where both meet. 我只是使用这样一个事实:顶部和右边有两个不同边框的DOM元素会产生两条相遇的对角线。 Then put the height and width of the DOM-Element to zero and set the border-top-width to window-height and the border-right-width to window-width. 然后将DOM-Element的高度和宽度设置为零,并将border-top-width设置为window-height,将border-right-width设置为window-width。 Update it with JavaScript on resize... That's all. 在调整大小时用JavaScript更新它......就是这样。

I've put a container in the DOM 我在DOM中放了一个容器

<div id="diagonal_outer"><div id="diagonal"></div></div>

Following CSS is nessesary 以下CSS是必须的

div#diagonal_outer {
    position: fixed !important;
    position: absolute;
    overflow: hidden;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    z-index: -100;
}
div#diagonal {
    position: relative;

    border-color: #FAE9E1 #ffffff;
    border-style: solid;
    border-left-width: 0;       
    border-top-width: 10240px;
    border-right-width: 12800px;
    border-bottom-width: 0;

    height: 0;
    width: 0;

    left: 50%;
    top: 50%;
    margin-left: -6400px; /* half of border-right-width */
    margin-top: -5120px; /* half of border-top-width */

    z-index: -100;
}

and following JavaScript to actualize on resize 并按照JavaScript来实现调整大小

jQuery(document).ready(function() {
    diagonal();
});

jQuery(window).resize(function() {
    diagonal();
});

var diagonal = function() {
    var wWidth = jQuery(window).width();
    var wHeight = jQuery(window).height();

    jQuery('#diagonal').css('left', 0);
    jQuery('#diagonal').css('top', 0);
    jQuery('#diagonal').css('margin-left', 0);
    jQuery('#diagonal').css('margin-top', 0);

    jQuery('#diagonal').css('border-right-width', wWidth);
    jQuery('#diagonal').css('border-top-width', wHeight);
};

OK, the solution with CSS-skew is nice, but this one works with CSS <3 好吧, CSS-skew的解决方案很不错,但是这个解决方案适用于CSS <3

You don't have to do too much for this. 你不必为此做太多。 See demo here 在这里演示

HTML HTML

<div class="diagonal"></div>

CSS CSS

.diagonal {
    width: 0; 
    height: 0; 
    border-top: 110px solid transparent;
    border-right:110px solid blue; 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想创建一个 div img 继续在右上角和左下角的网格像素周围 - I want to create a div img that continue around grid pixel in right-top and left-bottom 如何将文本位于左下角的<td>标签内的图像位置设置在左上角 - How to set image position inside <td> tag to be on top-left corner while text is on the left-bottom Fabric Js:如何仅以2个点(即左上角和右下角)以编程方式创建矩形? - Fabric Js: How to Create Rectangle programatically with only 2 points that is Top Left Corner and Bottom Right Corner? 在jQuery中将滚动移动到DIV右上角的功能 - Function to move scroll to the right-top corner of DIV in jQuery 右上角和左下角的半边框与CSS - half borders at top right corner and bottom left corner with css 我想以简单的html显示图像从右上角移动到左下角 - I want to show a image moving from right top corner to left bottom corner in simple html 如何正确地从各个侧面(顶部,左侧,底部,右侧)创建2d对象碰撞 - How do I create 2d object collision from every side(top,left,bottom,right) properly 如何从右到左以及从上到下调整div元素的大小 - How to resize div element from right to left and top to bottom 从一个角落到另一个角落的对角线动画 - Animating a diagonal line from corner to corner Galleria滑块从左到右从上到下 - Galleria slider from left—right to top—bottom
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM