代码之家  ›  专栏  ›  技术社区  ›  henriquehbr

使绝对定位的DIV覆盖视区

  •  0
  • henriquehbr  · 技术社区  · 7 年前

    我正在制作一个jquery动画,它有一个绝对定位的DIV,该DIV是在视区的特定位置生成的。( left: 150px )这个分区垂直和水平地增长,覆盖了整个视区。

    但不幸的是,DIV溢出了视区,没有覆盖整个屏幕。

    $(document).ready(function() {
    
    	function generateChild(top, bottom, left, right) {
    		$("#divGenerator").append(`
    			<div style="top:${top};bottom:${bottom};left:${left};right:${right};" class="child"></div>
    		`);
    	}
    
    	generateChild("50vh", "0", "80vw", "0");
    
    	/*
    	setInterval(function() {
    		$(".child").animate({
    			"transform": "translate3d(0, 0, 0)",
    			"transition": "all 0.5s ease-out"
    		}, 3000, function() {
    			$(this).fadeOut().remove();
    		});
    	}, 1000);
    	*/
    })
    body {
    	margin: 0;
    	overflow: hidden;
    }
    
    #divGenerator {
    	position: relative;
    	height: 100vh;
    	top: 0;
    	bottom: 0;
    	left: 0;
    	right: 0;
    	margin: auto;
    	background-color: black;
    	border: 1px solid black;
    }
    
    .child {
    	position: absolute;
    	width: 20px;
    	height: 20px;
    	border: 1px solid white;
    }
    <!DOCTYPE html>
    <html>
    <head>
    	<meta charset="utf-8">
    	<title>Trippy Waves</title>
    	<meta name="viewport" content="width=device-width, initial-scale=1.0">
    	<link rel="stylesheet" href="index.css">
    </head>
    <body>
    
    	<div id="divGenerator"></div>
    
    	<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    	<script src="index.js"></script>
    </body>
    </html>
    1 回复  |  直到 7 年前
        1
  •  2
  •   tao    7 年前

    为了达到这一效果,您需要几个条件:

    • 使用 position:fixed (不是) absolute ,因此元素是相对于视区定位的,而不是相对于其最近的相对定位父对象。
    • 获取初始值 top , left , width height .getBoundingClientRect() 从你开始的元素。注意,这个方法是在dom元素上定义的,而不是在jquery包装器上定义的(因此您需要 $(selector).get(0).getBoundingClientRect() )
    • 从这些位置到: top:0;left:0;width:100vw;height:100vh .

    在这个特定的例子中,您不需要 getBoundingClientRect() ,因为您从任意数据(从您的函数)生成您的元素——但通常您希望它得到元素的位置。 相对于视区的当前位置 (包括卷轴等…)。

    $(document).ready(function() {
    
        function generateChild(top, bottom, left, right) {
            $("#divGenerator").append(`
                <div style="top:${top};bottom:${bottom};left:${left};right:${right};" class="child"></div>
            `);
        }
    
        generateChild("50vh", "0", "80vw", "0");
    
        setInterval(function() {
            $(".child").animate({
                "width": "100vw",
                "height": "100vh",
               "top":0,
               "left":0
            }, 3000, function() {
                $(this).fadeOut().remove();
            });
        }, 1000);
    })
    body {
        margin: 0;
        overflow: hidden;
    }
    
    #divGenerator {
        position: relative;
        height: 100vh;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        margin: auto;
        background-color: black;
        border: 1px solid black;
    }
    
    .child {
        position: fixed;
        width: 20px;
        height: 20px;
        border: 1px solid white;
    }
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <title>Trippy Waves</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="index.css">
    </head>
    <body>
    
        <div id="divGenerator"></div>
    
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <script src="index.js"></script>
    </body>
    </html>

    个人建议: 不要使用 .animate() . 使用 velocity .