페이지가 다시로드 될 때마다 div id = "img"의 배경 전체 이미지가 변경되기를 원합니다. 순수 CSS에서 수행 할 수 있습니까? 아니면 자바 스크립트가 필요합니까?. 다음은 내 http://jsfiddle.net/9Dp2e/ 입니다. jquery와 함께 javascript를 사용하여 배경 이미지를 임의로 변경하고 있지만 아래 코드를 사용하여 작동하지 않습니다. 어떤 도움을 주시면 감사하겠습니다.
Here is my html:
<div id="header">
<ul>
<div id="wrapper">
<li class="logo"></li>
<li>Homes</li>
<li>Offices</li>
<li>Products</li>
<li></li>
<li></li>
</div>
</ul>
</div>
<div id="center">
<div id="slider">
<div id='footercopy'>
<p>Copyright 2014  <a href=""></a></p>
</div>
<div id="img">
<img src="http://upload.wikimedia.org/wikipedia/commons/e/e7/Flaming_cocktails.jpg">
</div>
</body>
</html>
Here is my css:
html,body
{
height:100%;
}
body
{
font-family: 'Open Sans Condensed', sans-serif;
}
h1
{
font-size:30px;
}
#img
{
position: fixed;
top: -50%;
left: -50%;
width: 200%;
height: 200%;
}
#img img
{
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
min-width: 50%;
min-height: 50%;
}
#center
{
position:relative;
z-index:10;
top:0;
min-height:1000px;
background:white;
width:960px;
margin-right:auto;
margin-left:auto;
min-height:1000px;
padding:20px;
}
#header
{
position:relative;
z-index:20;
color:white;
position:fixed;
top:0;
width:1700px;
margin-right:auto;
margin-left:-10px;
background:silver;
opacity:0.7;
}
#wrapper
{
width:960px;
margin-right:auto;
margin-left:auto;
}
#header li
{
display:inline;
padding-right:20px;
}
.logo
{
font-family: 'Lobster', cursive;
font-size:30px;
color:blue;
}
/*content*/
#content
{
padding-top:80px;
width:500px;
}
.floatLeft
{
float:left;
margin: 4px;
}
.floatRight
{
float: right;
margin: 4px;
}
#content img
{
width:100px;
height:100px;
padding:20px;
}
.width
{
width:250px;
padding:10px 30px;
}
#sidebar
{
margin-top:50px;
float:right;
min-height:1500px;
text-align:center;
}
#sidebar img
{
width:150px;
height:150px;
}
#sidebar li
{
list-style:none;
padding:10px;
}
#content-right
{
padding-top:80px;
width:450px;
float:right;
}
#content-right img
{
width:100px;
height:100px;
padding:20px;
}
Here is my javascript:
var images = ['http://acdutyfree.com/images/Page95_LiquorAndTobacco_BombaySapphire_DistilledLondonDryGin_800x800.jpg', 'http://images.china.cn/attachement/jpg/site1007/20130621/0013729929f1132dee4303.jpg', ];
$('#header').css({'background-image': 'url(images/' + images[Math.floor(Math.random() * images.length)] + ')'});
$('<img src="http://acdutyfree.com/images/Page95_LiquorAndTobacco_BombaySapphire_DistilledLondonDryGin_800x800.jpg' + images[Math.floor(Math.random() * images.length)] + '">').appendTo('#img');
당신은 이것을 사용할 수 있습니다 ... 완벽하게 잘 작동합니다. 방금 구성했습니다 :)
이것은 jQuery가 없습니다. 페이지에 무거운 스크립트를로드 할 필요가 없습니다. :)
//You can increase the images by adding more links to the array
var array = ['http://acdutyfree.com/images/Page95_LiquorAndTobacco_BombaySapphire_DistilledLondonDryGin_800x800.jpg','http://images.china.cn/attachement/jpg/site1007/20130621/0013729929f1132dee4303.jpg'];
function shuffle(array) {
var currentIndex = array.length;
var temporaryValue;
var randomIndex;
while (0 !== currentIndex) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
var shuffled_images = shuffle(array);
var yourbackground = document.getElementById('img');
yourbackground.style.backgroundImage = 'url(' + shuffled_images[0] + ')' ;
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다