简体   繁体   English

悬停时背景图片不变

[英]Background image does not change on hover

I have written the following code to change the background image of a div on mouse hover on another area. 我编写了以下代码来更改将鼠标悬停在另一个区域上时div的背景图像。 The image does not change. 图像不会改变。

function upDate(previewPic) { 
    document.getElementById("image").innerHTML=previewPic.alt;
    document.getElementById("image").style.backgroundImage = "url('+previewPic.src+')";
}

HTML: HTML:

<div id="image">
    Hover over an image below to display here.
</div>

<img class="preview" alt="Styling with a Bandana" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/389177/bacon.jpg" onmouseover="upDate(this)" onmouseout="unDo()">

<img class="preview" alt="With My Boy" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/389177/bacon2.JPG" onmouseover="upDate(this)" onmouseout="unDo()">

<img class="preview" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/389177/bacon3.jpg" alt="Young Puppy" onmouseover="upDate(this)" onmouseout="unDo()">

CSS: CSS:

/*Name this external file gallery.css*/
body {
    margin: 2%;
    border: 1px solid black;
    background-color: #b3b3b3;
}

#image {
    line-height:650px;
    width: 575px;
    height: 650px;
    border:5px solid black;
    margin:0 auto;
    background-color: #8e68ff;
    background-image: url('');
    background-repeat: no-repeat;
    color:#FFFFFF;
    text-align: center;
    background-size: 100%;
    margin-bottom:25px;
    font-size: 150%;
}

.preview {
    width:10%;
    margin-left:17%;
    border: 10px solid black;
}

img {
        width:95%;
}

JavaScript: JavaScript:

/*Name this external file gallery.js*/

function upDate(previewPic) {
    document.getElementById("image").innerHTML=previewPic.alt;
    document.getElementById("image").style.backgroundImage = "url(' + previewPic.src + ')";
}

function unDo() {
    document.getElementById("image").innerHTML="Hover over an image below to display here.";
    document.getElementById("image").style.backgroundColor="8e68ff";
    document.getElementById("image").style.backgroundImage = "url('')";
}

Codepen 码笔

Concatenate variable by escaping double quote or else variable name will be treated as string literal. 通过转义双引号来连接变量,否则变量名称将被视为string文字。

 /*Name this external file gallery.js*/ function upDate(previewPic) { document.getElementById("image").innerHTML = previewPic.alt; document.getElementById("image").style.backgroundImage = "url(\\"" + previewPic.src + "\\")"; } function unDo() { document.getElementById("image").innerHTML = "Hover over an image below to display here."; document.getElementById("image").style.backgroundColor = "8e68ff"; document.getElementById("image").style.backgroundImage = "url('')"; } 
 /*Name this external file gallery.css*/ body { margin: 2%; border: 1px solid black; background-color: #b3b3b3; } #image { line-height: 650px; width: 575px; height: 650px; border: 5px solid black; margin: 0 auto; background-color: #8e68ff; background-image: url(''); background-repeat: no-repeat; color: #FFFFFF; text-align: center; background-size: 100%; margin-bottom: 25px; font-size: 150%; } .preview { width: 10%; margin-left: 17%; border: 10px solid black; } img { width: 95%; } 
 <div id="image"> Hover over an image below to display here. </div> <img class="preview" alt="Styling with a Bandana" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/389177/bacon.jpg" onmouseover="upDate(this)" onmouseout="unDo()"> <img class="preview" alt="With My Boy" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/389177/bacon2.JPG" onmouseover="upDate(this)" onmouseout="unDo()"> <img class="preview" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/389177/bacon3.jpg" alt="Young Puppy" onmouseover="upDate(this)" onmouseout="unDo()"> 

If you are willing to modify the HTML slightly, this can be achieved using only CSS by using the adjacent element selector. 如果您愿意稍微修改HTML,则可以通过使用相邻的元素选择器仅使用CSS来实现。

Moving the thumbnails above the image in the HTML, you can use this selector to change the adjacent div's background. 将缩略图移到HTML中图像上方,您可以使用此选择器更改相邻div的背景。 Give each of the thumbnails an ID: 为每个缩略图指定一个ID:

<img class="preview" id="bacon" alt="Styling with a Bandana" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/389177/bacon.jpg">

<img class="preview" id="bacon2" alt="With My Boy" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/389177/bacon2.JPG" >

<img class="preview" id="bacon3" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/389177/bacon3.jpg" alt="Young Puppy">

<div id="image">
  <span>    
     Hover over an image below to display here.
  </span>
</div>

And use the ~ selector in the CSS: 并在CSS中使用〜选择器:

#bacon:hover ~ div {  // The selector will change the adjacent div on hover
   background: url("https://s3-us-west-2.amazonaws.com/s.cdpn.io/389177/bacon.jpg");
}
#bacon2:hover ~ div {
   background: url("https://s3-us-west-2.amazonaws.com/s.cdpn.io/389177/bacon2.JPG");
}
#bacon3:hover ~ div {
   background: url("https://s3-us-west-2.amazonaws.com/s.cdpn.io/389177/bacon3.jpg");
}

You can re-position the elements to match the original layout, see this jsFiddle for a working example. 您可以重新定位元素以匹配原始布局,有关工作示例,请参见此jsFiddle

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

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