简体   繁体   English

倾斜的div不需要的透明空间

[英]Unwanted transparent space with skewed div

I'm trying to create a site with skewed/rotated divs, but I've got a problem with the ones that have fixed background image. 我正在尝试创建一个具有倾斜/旋转div的网站,但是对于背景图片固定的网站,我遇到了问题。

The code describes the issue best: 该代码最能说明问题:

https://codepen.io/poveu/pen/pWJwYx https://codepen.io/poveu/pen/pWJwYx

<div class="skewed_fixed">
        <div class="content">
      This parent's background is fixed. When you scroll down, there's still that white body background above, and this text scrolls into it.
    </div>
</div>
<div class="skewed_normal">
    <div class="content">
      This background is not fixed and behaves properly.
    </div>
</div>
<div class="margin"></div>

.skewed_fixed {
    background: url(http://cdn3-www.dogtime.com/assets/uploads/gallery/goldador-dog-breed-pictures/puppy-1.jpg) no-repeat;
    background-attachment: fixed;
    transform: skewY(-3deg);
    background-size: cover;
    width: 100%;
}

.content {
    height: 300px;
}

.skewed_normal {
    background: url(http://cdn3-www.dogtime.com/assets/uploads/gallery/goldador-dog-breed-pictures/puppy-1.jpg) no-repeat;
    transform: skewY(-3deg);
    background-size: cover;
    width: 100%;
}

.margin {
    height: 600px;
}

(background-size: cover and width: 100% is here just to make the bg fit 100% width; margin div is to make the page scrollable) (background-size:cover和width:100%只是为了使bg适合100%宽度; margin div是使页面可滚动)

I want to scroll the whole div with its background "mask" so the white space disappears when scrolling down. 我要滚动整个div及其背景“蒙版”,以便在向下滚动时空白消失。

I've tried to mix it with transform-origin: left; 我尝试将其与transform-origin混合: but then the transparent space appears on the bottom of the div. 但是透明空间出现在div的底部。

Is there any easy (non JS) way to achieve what I want? 有什么简单的方法(非JS)可以实现我想要的?

You could play on positions and margin or padding to compensate the empty space : 您可以在位置,边距或边距上进行游戏以补偿空白:

 .skewed_fixed {
    background: url(http://cdn3-www.dogtime.com/assets/uploads/gallery/goldador-dog-breed-pictures/puppy-1.jpg) no-repeat;
    background-attachment: fixed;
    transform: skewY(-3deg);
    background-size: cover;
    width: 100%;
    background-position: 0px -100px;
    top:-50px;
    position:relative;
    margin-top:50px;
}

I had used translate property with skew property. 我使用了带有倾斜属性的翻译属性。 Try this. 尝试这个。

 .skewed_fixed { background: url(http://cdn3-www.dogtime.com/assets/uploads/gallery/goldador-dog-breed-pictures/puppy-1.jpg) no-repeat; background-attachment: fixed; transform: skewY(-3deg) translate(0px, -50px); background-size: cover; width: 100%; } .content { height: 300px; padding-top: 60px; } .skewed_normal { background: url(http://cdn3-www.dogtime.com/assets/uploads/gallery/goldador-dog-breed-pictures/puppy-1.jpg) no-repeat; transform: skewY(-3deg) translate(0px, -50px); background-size: cover; width: 100%; } .margin { height: 600px; } 
 <div class="skewed_fixed"> <div class="content"> This parent's background is fixed. When you scroll down, there's still that white body background above, and this text scrolls into it. </div> </div> <div class="skewed_normal"> <div class="content"> This background is not fixed and behaves properly. </div> </div> <div class="margin"></div> 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM