简体   繁体   English

响应偏斜的Div与背景图像

[英]Responsive Skewed Div with background image

I am attempting to make a page where the screen is split in half with two images from the bottom right corner to the top left corner 我试图制作一个页面,屏幕分成两半,从右下角到左上角有两个图像

在此输入图像描述 I have done this in CSS using transform: skewY( x amount deg); 我在CSS中使用transform:skewY(x amount deg); I can then change this with javascript when the page loads by calculating the degree needed via trigonometry like so 然后我可以在页面加载时通过javascript计算所需的度数来改变这个

var hlc = document.getElementById('homeleftside');
var hlch = hlc.clientHeight;
var hlcw = hlc.clientWidth;
var hlct = Math.atan(hlch/hlcw);
var hlca = hlct * 180 / Math.PI;

and I can do this via javascript every time the page is resized, but to make this in CSS I have made these classes below and was wondering if there is a better alternative to a responsive degree amount depending on the page size due to editing the pseudo:: after element. 每次页面调整大小时我都可以通过javascript做到这一点,但是为了在CSS中创建这个我已经在下面创建了这些类,并且想知道是否有更好的替代响应度数量取决于页面大小由于编辑伪::元素之后。

.homeleftside::after {
    transform-origin: top left;
    transform: skewY(-29deg);
    content: '';
    height: 100%;
    width: 100%;
    background: url("graphics/architecture.jpg");
    color: #fff;
    position:absolute;
    top: 0px;
    left: 0px;
    z-index: 1;
    overflow: hidden;
}


.homeleftside {
    height: 100%;
    width: 100%;
    position: absolute;
    top: 0px;
    left: 0px;
    overflow: hidden;
    transform-origin: top left;
    transform: skewY(29deg);
}

As far as I know, your only posibility is with a mask-image. 据我所知,你唯一的可能性是掩模图像。

Support is not fully, but it gives an easy way to achieve it. 支持不完全,但它提供了一种简单的方法来实现它。

Note that the direction "top left" (and similars) for a gradient will get you always the diagonal of the element 请注意,渐变的“左上角”(和类似物)方向将使您始终成为元素的对角线

 .test { background-image: linear-gradient(red, green); -webkit-mask-image: linear-gradient(to top right, black 50%, transparent 50%); mask-image: linear-gradient(to top right, black 50%, transparent 50%); } #test1 { width: 300px; height: 200px; } #test2 { width: 200px; height: 200px; } 
 <div class="test" id="test1"></div> <div class="test" id="test2"></div> 

You can easily achieve this using clip-path 您可以使用clip-path轻松实现此目的

 body { margin:0; height:100vh; background:url(https://picsum.photos/id/10/800/800) center/cover; } body:before { content:""; display:block; height:100%; background:url(https://picsum.photos/id/18/800/800) center/cover; -webkit-clip-path:polygon(0 0,0 100%,100% 100%); clip-path:polygon(0 0,0 100%,100% 100%); } 

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

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