Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Css 带有文本的网页上的对角线图像_Css - Fatal编程技术网

Css 带有文本的网页上的对角线图像

Css 带有文本的网页上的对角线图像,css,Css,以下是我试图达到的效果: 当用户将鼠标移到图像上时,一行文本应以对角线方式覆盖图像 图像可以是的背景。真的只是需要帮助,首先使整个事情对角线。不希望使用硬编码尺寸/位置,这些尺寸/位置在不同宽度/高度的屏幕上不起作用 <div class="testrows"> <div class="drow"><p>Hello World</p></div> <div class="drow"><p>Hello

以下是我试图达到的效果:

当用户将鼠标移到图像上时,一行文本应以对角线方式覆盖图像

图像可以是
的背景。真的只是需要帮助,首先使整个事情对角线。不希望使用硬编码尺寸/位置,这些尺寸/位置在不同宽度/高度的屏幕上不起作用

<div class="testrows">  
  <div class="drow"><p>Hello World</p></div>
  <div class="drow"><p>Hello World</p></div>
  <div class="drowhalf">
    <p>Hello World</p><p>Hello World</p>
  </div>
  <div class="drowhalf">
    <p>Hello World</p><p>Hello World</p>
  </div>
  <div class="drow"><p>Hello World</p></div>
  <div class="drow"><p>Hello World</p></div>
</div>

使用旋转不是更好吗? 我想这可能是你的答案:

CSS:

body {
  background: #e5e5e5;
}

.aviso {
  width: 50px;
  height: 200px;
  background: black;
  position: absolute;
  top: -50px;
  left: 0;
  -ms-transform: rotate(45deg);
  -webkit-transform: rotate(45deg);
  transform: rotate(45deg);
  text-align: center;
  color: #fff;
  vertical-align: middle;
}

.aviso p {
  ms-transform: rotate(-90deg);
  -webkit-transform: rotate(-90deg);
  transform: rotate(-90deg);
  padding-right: 60px;
  width: 100%;
  padding-bottom: 55px;
}
<div class="aviso">
  <p>WIP</p>
</div>
HTML:

body {
  background: #e5e5e5;
}

.aviso {
  width: 50px;
  height: 200px;
  background: black;
  position: absolute;
  top: -50px;
  left: 0;
  -ms-transform: rotate(45deg);
  -webkit-transform: rotate(45deg);
  transform: rotate(45deg);
  text-align: center;
  color: #fff;
  vertical-align: middle;
}

.aviso p {
  ms-transform: rotate(-90deg);
  -webkit-transform: rotate(-90deg);
  transform: rotate(-90deg);
  padding-right: 60px;
  width: 100%;
  padding-bottom: 55px;
}
<div class="aviso">
  <p>WIP</p>
</div>

在制品

$(“div”).hover(函数(){
$('p').css('display','block');
},函数(){
$('p').css('display','none');
});
div{
-webkit变换:旋转(-30度);
-moz变换:旋转(-30度);
-ms变换:旋转(-30度);
-o变换:旋转(-30度);
变换:旋转(-30度);
利润率:100像素;
高度:150像素;
宽度:350px;
背景图像:url('http://placehold.it/350x150');
}
p{显示:无}

你好,世界


使用
旋转
而不是
倾斜

.div {
    width: 200px;
    height: 200px;  
    -webkit-transform: rotate(45deg);
       -moz-transform: rotate(45deg);
        -ms-transform: rotate(45deg);
         -o-transform: rotate(45deg);
            transform: rotate(45deg);
}

您可以用一个包装器(
.internal wrapper
)包装所有
.drow
元素,然后旋转它(),设置为
左上
以从元素的
左上
旋转,最后将
translateX(-50%)
置于
内包装器
以使其在其父元素中居中

对于拉伸
.drow
,您可以将
宽度:200%
赋予
.inner wrapper

要计算
.drow
的高度,必须使用js

函数拉伸(){
变量$wrapper=$('.wrapper'),
对角线=Math.ceil(Math.sqrt(Math.pow($wrapper.width(),2)+Math.pow($wrapper.height(),2)),
高度=对角线/$wrapper.find('.inner wrapper.drow')。长度;
$wrapper.find('.inner wrapper.drow').height(height).css('line-height',height+'px');
}
$(文档).ready(函数(){
拉伸();
});
$(窗口)。调整大小(函数(){
拉伸();
});
html,
身体{
保证金:0;
填充:0;
}
html,
身体,
.wrapper,
.内包装{
身高:100%;
}
身体{
背景:#e5;
}
p{
保证金:0;
}
.包装纸{
溢出:隐藏;
}
.内包装{
变换:旋转(-45度)translateX(-50%);
变换原点:左上角;
文本对齐:居中;
宽度:200%;
}
卓尔先生{
高度:100px;
线高:100px;
颜色:#fff;
背景:rgba(0,0,0,1);
}
卓沃尔夫先生{
显示:内联块;
宽度:50%;
}

你好,世界

你好,世界

你好世界

你好世界

你好世界

你好世界

你好,世界

你好,世界


您可以使用纯css实现此效果

div{
利润率:100像素;
高度:150像素;
宽度:350px;
背景图像:url('http://placehold.it/350x150');
}
分区:悬停p{
显示:块;
}
p{
显示:无;
-webkit变换:旋转(-30度);
-moz变换:旋转(-30度);
-ms变换:旋转(-30度);
-o变换:旋转(-30度);
变换:旋转(-30度);
}

你好,世界


好的,所以只能使用Javascript和jquery。没有CSS方法可以做到这一点

js

        <script type="text/javascript">
        $(window).on('resize', function (){
            updateWidth();
        });

        function toRadians (angle) {
          return angle * (Math.PI / 180);
        }

        function updateWidth(){
            //add this to the width to make sure it fits
            var idealRatio = 45.0/114;
            idealHeight = 134.0;
            var safetyAdd = 10;
            //var topMargin = .;
            var divHeightPercent = 1/6.4;

            //first get the new content container height and width
            var contentHeight = $('#content').height();
            console.log('height:' + contentHeight);
            var contentWidth = $('#content').width();
            console.log('width:' + contentWidth);

            //scale the hieght in proportion to the diagonal
            /*
            var proportion = Math.sqrt(contentHeight*contentHeight+ contentWidth*contentWidth)/(contentHeight/Math.cos(toRadians(20)));
            console.log('proportion:' + proportion);
            var theta = Math.atan(contentWidth/contentHeight);
            var degree = 180*theta/Math.PI;
            console.log('degree:' + degree);*/

            //calculate the correction factor for height
            var correctionFactor= (contentHeight*1.0/Math.cos(toRadians(20)))/contentHeight;
            correctionFactor= (idealRatio/(contentHeight/contentWidth));
            correctionFactor= 1+correctionFactor;
            console.log('correctionFactor:' + correctionFactor);
            divHeightPercent = divHeightPercent*correctionFactor;
            console.log('divHeightPercent:' + divHeightPercent);
            //topMargin=topMargin*correctionFactor;
            //console.log('topMargin:' + topMargin);

            //calculate the height of each section
            var commonDivHeight = contentHeight*divHeightPercent;
            console.log('divHeight:' + commonDivHeight);
            $("#drow0").height(commonDivHeight);
            $("#drow1").height(commonDivHeight);
            $("#drow2").height(commonDivHeight);
            $("#drow3").height(commonDivHeight);
            $("#drow4").height(commonDivHeight);
            $("#drow5").height(commonDivHeight);



            //correctio for common div height
            var cdhCorr = Math.tan(toRadians(20))*commonDivHeight;
            cdhCorr = Math.sin(toRadians(20))*cdhCorr;
            console.log('cdhCorr:' + cdhCorr);
            var diagonalHeight = commonDivHeight/Math.cos(toRadians(20));
            console.log('diagonalHeight:' + diagonalHeight);

            //calculate the bottom for the first div and proper width
            var zeroRowBotPoint = diagonalHeight;
            console.log('zeroRowBotPoint:' + zeroRowBotPoint);
            //var zeroRowWidth = zeroRowBotPoint/Math.sin(toRadians(20));//apply some correction faction here
            //console.log('zeroRowWidth:' + firstRowWidth);
            var zeroRowWidth = calcMaxWidthNeeded(zeroRowBotPoint, contentWidth, commonDivHeight);
            $("#drow0").width(zeroRowWidth);
            $("#drow0").css({top: 0});

            //calculate the bottom for the first div and proper width
            var firstRowBotPoint = diagonalHeight+zeroRowBotPoint;
            console.log('firstRowBotPoint:' + firstRowBotPoint);
            //var firstRowWidth = firstRowBotPoint/Math.sin(toRadians(20));//apply some correction faction here
            //console.log('firstRowWidth:' + firstRowWidth);
            var firstRowWidth = calcMaxWidthNeeded(firstRowBotPoint, contentWidth, commonDivHeight);
            $("#drow1").width(firstRowWidth);
            var row1padding = getLeftPadding(contentHeight, firstRowBotPoint, diagonalHeight, commonDivHeight);
            $("#drow1").css({top: zeroRowBotPoint, paddingLeft: row1padding, marginRight:700});

            //calculate the bottom for the second div and proper width
            var secondRowBotPoint = diagonalHeight+firstRowBotPoint;
            console.log('secondRowBotPoint:' + secondRowBotPoint);
            //var secondRowWidth = secondRowBotPoint/Math.sin(toRadians(20));//apply some correction faction here
            //console.log('secondRowWidth:' + secondRowWidth);
            var secondRowWidth = calcMaxWidthNeeded(secondRowBotPoint, contentWidth, commonDivHeight);
            $("#drow2").width(secondRowWidth);
            var row2padding = getLeftPadding(contentHeight, secondRowBotPoint, diagonalHeight, commonDivHeight);
            $("#drow2").css({top: firstRowBotPoint, paddingLeft: row2padding});

            //calculate the bottom for the third div and proper width
            var thirdRowBotPoint = diagonalHeight+secondRowBotPoint;
            console.log('thirdRowBotPoint:' + thirdRowBotPoint);
            //var thirdRowWidth = thirdRowBotPoint/Math.sin(toRadians(20));//apply some correction faction here
            //console.log('thirdRowWidth:' + thirdRowWidth);
            var thirdRowWidth = calcMaxWidthNeeded(thirdRowBotPoint, contentWidth, commonDivHeight);
            $("#drow3").width(thirdRowWidth);
            var row3padding = getLeftPadding(contentHeight, thirdRowBotPoint, diagonalHeight, commonDivHeight);
            $("#drow3").css({top: secondRowBotPoint, paddingLeft: row3padding});

            //calculate the bottom for the forth div and proper width
            var forthRowBotPoint = diagonalHeight+thirdRowBotPoint;
            console.log('forthRowBotPoint:' + forthRowBotPoint);
            //var forthRowWidth = forthRowBotPoint/Math.sin(toRadians(20));//apply some correction faction here
            //console.log('forthRowWidth:' + forthRowWidth);
            var row4padding = getLeftPadding(contentHeight, forthRowBotPoint, diagonalHeight, commonDivHeight);
            var forthRowWidth = calcMaxWidthNeeded(forthRowBotPoint, contentWidth, commonDivHeight)- row4padding;
            $("#drow4").width(forthRowWidth);
            var topCorrection4 = row4padding*Math.tan(toRadians(20));
            console.log('topCorrection4:' + topCorrection4);
            $("#drow4").css({top: thirdRowBotPoint-topCorrection4, left: row4padding});

            //calculate the bottom for the fith div and proper width
            var fifthRowBotPoint = diagonalHeight+forthRowBotPoint;
            console.log('fifthRowBotPoint:' + fifthRowBotPoint);
            //var fifthRowWidth = fifthRowBotPoint/Math.sin(toRadians(20)) - row5padding;//apply some correction faction here
            //console.log('fifthRowWidth:' + fifthRowWidth);
            var row5padding = getLeftPadding(contentHeight, fifthRowBotPoint, diagonalHeight, commonDivHeight);
            var fifthRowWidth = calcMaxWidthNeeded(fifthRowBotPoint, contentWidth, commonDivHeight)- row5padding;
            var topCorrection5 = row5padding*Math.tan(toRadians(20));
            $("#drow5").width(fifthRowWidth);
            $("#drow5").css({top: forthRowBotPoint-topCorrection5, left: row5padding});

            /*correct the padding
            //var y = contentHeight;
            var row0padding = 0; //its not possible
            var row1padding = getLeftPadding(contentHeight, firstRowBotPoint, diagonalHeight, commonDivHeight);
            var row2padding = getLeftPadding(contentHeight, secondRowBotPoint, diagonalHeight, commonDivHeight);
            var row3padding = getLeftPadding(contentHeight, thirdRowBotPoint, diagonalHeight, commonDivHeight);
            var row4padding = getLeftPadding(contentHeight, forthRowBotPoint, diagonalHeight, commonDivHeight);
            var row5padding = getLeftPadding(contentHeight, fifthRowBotPoint, diagonalHeight, commonDivHeight);
            //*/

             //get font height
            var topPadding = (commonDivHeight-32)/2;
            $("#rowp1").css({marginTop: topPadding, marginLeft:-250});
            $("#rowp2").css({marginTop: topPadding});
            $("#rowp3").css({marginTop: topPadding});
            $("#rowp4").css({marginTop: topPadding});
            $("#rowp5").css({marginTop: topPadding});
            $("#rowp6").css({marginTop: topPadding, marginLeft:250});

            //finall unhide the contents
            $("#content").css({opacity:1});
        }

        function calcMaxWidthNeeded (bottomLoc, contentWidth, height){
            //first find the width of the triangle formed by the section
            var xDistance = bottomLoc/Math.tan(toRadians(20));

            if(xDistance <= contentWidth){
                //the width calculated by the simple function will be most efficient
                console.log('Using simple Width Calculation');
                return bottomLoc/Math.sin(toRadians(20));
            }

            var x = xDistance - contentWidth;
            var bigHyp = xDistance/Math.cos(toRadians(20));
            var ratio = x/xDistance;

            var smallHyp = bigHyp*ratio;

            //get the extra width due to the 20 degree rotaion
            var extraWidth = height*Math.tan(toRadians(20));

            var efficientWidth = bigHyp-smallHyp+extraWidth;
            console.log('efficientWidth:' + efficientWidth);
            return efficientWidth;

        }

        function getLeftPadding(contentHeight, bottomLoc, diagonalHeight, height){
            var y = bottomLoc - contentHeight;
            var x = y-diagonalHeight;

            //return if there is no need to change the padding
            if(x<=0){
                console.log('Left padding change not required');
                return 0;
            }

            var bottHyp =  y/Math.sin(toRadians(20));
            var xtoyRatio = x/y;
            var topHyp = bottHyp*xtoyRatio;

            //get the extra width due to the 20 degree rotaion
            var extraWidth = height*Math.tan(toRadians(20));
            console.log('extraWidth:' + extraWidth);

            //finally calculate the padding
            var paddingVal = topHyp ;

            console.log('paddingVal:' + paddingVal);
            return paddingVal;
        }

        function myFunction() {
            document.querySelector('#selected').style.color='#008ed2';

            updateWidth();
        }
    </script>

您希望旋转而不是扭曲共享的链接可能有助于找到问题的答案您应该始终尝试在答案中包含最少的工作(代码)示例。请记住,链接往往会更改并过期。@请不要以这种方式使用
setInterval
,这样会不必要地迫使浏览器反复运行代码。使用
$('div').hover(函数(){$('p').css('display','block');},函数(){$('p').css('display','none');})尝试使用此示例创建一个网格,就像我在问题中指出的那样。我已经根据大家提供的帮助更新了我的问题。这确实很好地旋转了它。任何确保高度和宽度始终充满窗户的方法(100%)。我需要确保即使在旋转后,行也会拉伸以占据整个宽度。让一些js运行起来会更容易吗?@codeNinja您可以提供一个安全宽度(
.inner wrapper
)来拉伸
.drow
,例如,
宽度:200%
但是对于
高度
.drow
你必须使用js。请检查编辑后的答案。
    .p, .drow1, .drow2, .drow3,.drow4{
    margin: 0;

}

.content {
    overflow: show;
    position:relative;
    text-align: center;
    background-color: black;

    color: white;
    font-family: 'futura-pt-bold', sans-serif;
    font-style: normal;
    font-weight: 700;
    font-size: 2em;
    text-shadow:0px 0px 16px black;
    opacity: 0;
}

.drow0, .drow1, .drow2, .drow3, .drow4, .drow5{
    position:absolute;
    transform: rotate(-20deg);
    transform-origin: bottom left;
    height:16.66%;
    left:0%;
    width: 100%;
    background-repeat:no-repeat;
    background-size:cover;
}

.drow0{
    top:0%;
    background-image: url("../desktop_images/1.jpg");
}

.drow1{
    top:16.66%;
    background-image: url("../desktop_images/2.jpg");
}

.drow2{
    display: inline-flex;
    top:33.33%;
}

.drow2-1{
    width: 60%;
    background-image: url("../desktop_images/3.jpg");
    background-repeat:no-repeat;
    background-size:cover;
}
.drow2-2{
    width: 40%;
    background-image: url("../desktop_images/4.jpg");
    background-repeat:no-repeat;
    background-size:cover;
}

.drow3{
    display: inline-flex;
    top:50%;
}

.drow3-1{
    width: 45%;
    background-image: url("../desktop_images/5.jpg");
    background-repeat:no-repeat;
    background-size:cover;
}
.drow3-2{
    width: 55%;
    background-image: url("../desktop_images/6.jpg");
    background-repeat:no-repeat;
    background-size:cover;
}

.drow4{
    background-image: url("../desktop_images/7.jpg");
    top:66.66%;
}

.drow5{
    background-image: url("../desktop_images/8.jpg");
    top:83.33%;
}

.drow1:hover { 
    background-repeat: no-repeat;
    background-position: center;
    color: transparent;
    text-shadow:0px 0px 0px transparent;

    background-image: url("../desktop_images/bg-2.png");
    background-size: auto 65%;
}

.drow2-1:hover { 
    background-repeat: no-repeat;
    background-position: center;
    color: transparent;
    text-shadow:0px 0px 0px transparent;

    background-image: url("../desktop_images/bg-3.png");
    background-size: 52% auto;
}

.drow2-2:hover { 
    background-repeat: no-repeat;
    background-position: center;
    color: transparent;
    text-shadow:0px 0px 0px transparent;

    background-image: url("../desktop_images/bg-4.png");
    background-size: 75% auto;
}

.drow3-1:hover { 
    background-repeat: no-repeat;
    background-position: center;
    color: transparent;
    text-shadow:0px 0px 0px transparent;

    background-image: url("../desktop_images/bg-5.png");
    background-size: auto 65%;
}

.drow3-2:hover { 
    background-repeat: no-repeat;
    background-position: center;
    color: transparent;
    text-shadow:0px 0px 0px transparent;

    background-image: url("../desktop_images/bg-6.png");
    background-size: auto 65%;
}

.drow4:hover { 
    background-repeat: no-repeat;
    background-position: center;
    color: transparent;
    text-shadow:0px 0px 0px transparent;

    background-image: url("../desktop_images/bg-7.png");
    background-size: auto 65%;
}