简体   繁体   English

如何在内部具有100%宽度/高度的背景图像 <li> ?

[英]How to have a backgroud image 100% width / height inside a <li>?

I have a slider with 4 items in it. 我有一个带有4个项目的滑块。

CSS 的CSS

#section_3 {height:600px;}
#section_3 ul {list-style:none; width:400%; margin:0; padding:0;}
#section_3 ul li {float:left; width:25%; display:block; }
#section_3 ul li.sec1{background:url('../imgs/bg1.jpg') no-repeat; height:600px;}
#section_3 ul li.sec2{background:url('../imgs/bg2.jpg') no-repeat; height:600px;}
#section_3 ul li.sec3{background:url('../imgs/bg1.jpg') no-repeat; height:600px;}
#section_3 ul li.sec4{background:url('../imgs/bg2.jpg') no-repeat; height:600px;}

HTML 的HTML

<div id="section_3" class="overflow">
        <ul id="slider">
            <li class="sec1">
                <h1>1</h1>
            </li>
            <li class="sec2">
                <h1>2</h1>
            </li>
            <li class="sec3">
                <h1>3</h1>
            </li>
            <li class="sec4">
                <h1>4</h1>
            </li>
        </ul>
    </div>

The slider is working perfectly but now I would like to make in sort that each LI have it's own background 100% width and 100% height. 滑块工作正常,但现在我想让每个LI拥有自己的100%宽度和100%高度的背景。

It could be in jQuery, javascript 可能是jQuery,javascript

How can I achieve that? 我该如何实现?

You can't scale CSS background images in a cross-browser manner. 您不能以跨浏览器的方式缩放CSS背景图像。 You will need to put the image in the html ( <img/> ), absolute position it inside the <li> to take it out of the flow, and use CSS (possibly even JS depending on your situation) to scale the image to 100% the size of the <li> . 您将需要将图像放在html( <img/> )中,将其绝对定位在<li> ,以使其脱离流程,并使用CSS(视情况而定甚至使用JS)将图像缩放至<li>大小的100%。

Try 尝试

#section_3 ul li.sec1{background:url('../imgs/bg1.jpg') no-repeat; height:600px; background-size: 100% 100%;}
#section_3 ul li.sec2{background:url('../imgs/bg2.jpg') no-repeat; height:600px; background-size: 100% 100%;}
#section_3 ul li.sec3{background:url('../imgs/bg1.jpg') no-repeat; height:600px; background-size: 100% 100%;}
#section_3 ul li.sec4{background:url('../imgs/bg2.jpg') no-repeat; height:600px; background-size: 100% 100%;}

It's a simple fix in CSS. 这是CSS中的简单修复。 If you wanted to do it in Javascript, try 如果您想使用Javascript进行操作,请尝试

document.getElementById("id_of_li").style.backgroundSize = "100% 100%";

Check out the w3schools tutorial: http://www.w3schools.com/cssref/css3_pr_background-size.asp 查看w3schools教程: http : //www.w3schools.com/cssref/css3_pr_background-size.asp

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

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