从昨天起我开始使用javascript来制作适用于页面正文的悬停效果(我希望在悬停div时更改页面正文的背景颜色),
我已经看了一些教程并且我自己尝试了但是我最终失败了,我想知道如何用javascript创建这个效果,我使用了这样的东西。
function mouseOver() {
document.getElementsByClassName("body").style.backgroundColor = "black"
}
function mouseOut() {
document.getElementsByClassName("body").style.backgroundColor = "white"
}

.college .image {
left: 100px;
top: 475px;
position: absolute
}
.college:hover .imagefirst {
opacity: 0.2;
}
.college .imagesecond {
width: 550px;
height: 900px;
transform: translate(-110px, 500px);
transition: transform 0.5s ease-in-out 0.25s;
border-radius: 8px;
overflow: hidden;
}
.college:hover>.imagesecond {
transform: translate(-110px, -500px);
}
.college:hover>body {
background-color: black
}
.lycee .image {
left: 700px;
top: 500px;
position: absolute
}
.lycee .imagefourth {
width: 537px;
height: 600px;
transform: translate(-160px, 500px);
transition: transform 0.5s ease-in-out 0.2s;
border-radius: 8px;
overflow: hidden;
}
.lycee:hover>.imagefourth {
transform: translate(-160px, -325px);
}
.formations .image {
left: 1250px;
top: 510px;
position: absolute;
}
.formations .imagesixth {
width: 550px;
height: 900px;
transform: translate(-100px, 400px);
transition: transform 0.5s ease-in-out 0.2s;
border-radius: 8px;
overflow: hidden
}
.formations:hover>.imagesixth {
transform: translate(-173px, -600px);
}
body {
background: url("accueil.png") 33em 0% fixed no-repeat;
position: fixed;
background-color: white/* rgb(0,85,170); */
}

<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="css.css" />
<title> sainte marie </title>
</head>
<body>
<div class="saintemarie">
<a href="college/collegesaintemarie.html">
<div onmouseover="mouseover()" onmouseout="mouseout()" class="college">
<img class="image imagefirst" src="http://via.placeholder.com/196x175" />
<img class="image imagesecond" src="http://via.placeholder.com/320x440" />
</div>
</a>
<a href="lycee/lyceesaintemarie.html">
<div class="lycee">
<img class="image imagethird" src="http://via.placeholder.com/183x140" />
<img class="image imagefourth" src="http://via.placeholder.com/320x440" />
</div>
</a>
<a href="c&formation/c&fsaintemarie.html">
<div class="formations">
<img class="image imagefifth" src="http://via.placeholder.com/172x153" />
<img class="image imagesixth" src="http://via.placeholder.com/320x440" />
</div>
</a>
</div>
</body>
</html>
&#13;
我不能使用jQuery orortherwise我不会首先尝试提出这个问题,我需要知道如何将JS文件中的悬停效果应用到我的html和css页面
-edit -
我已经被教会如何使用鼠标悬停和背景着色但我想问另一件事,它是关于不透明度,我想把不透明度放在其他div上我决定做同样的事情在我在document.lycee.style.opacity = "0.1";
和document.formations.style.opacity = "0.1";
之前所教的内容,但它实际上不起作用,就像document.body.style.opacity = "0.1";
一样,它改变了整个页面的不透明度。你能帮我最后一次吗?
答案 0 :(得分:2)
确保在内容后加载您的Javascript代码,并将onmouseOver
替换为onmouseover
。
document.getElementsByClassName("body").style.backgroundColor = "black"
你可以这样做
document.body.style.backgroundColor = 'red';
如果你想玩DIV&#39>
var divs = document.getElementsByTagName('div');
for(var i=0; i < divs.length; i++) {
divs[i].style.opacity = "0.8";
}
如果您只想更改具有className
的特定元素的样式var divs = document.getElementsByClassName('classNameHere');
for(var i=0; i < divs.length; i++) {
divs[i].style.opacity = "0.8";
}
编辑!
答案 1 :(得分:2)
正文不是一个类,所以这个声明不起作用,document.getElementsByClassName("body").style.backgroundColor = "black"
要选择正文,您可以使用document.body
或document.getElementsByTagName('body')[0];
function mouseOver() {
document.body.style.backgroundColor = "black"
}
function mouseOut() {
document.body.style.backgroundColor = "white"
}
.college .image {
left: 100px;
top: 475px;
position: absolute
}
.college:hover .imagefirst {
opacity: 0.2;
}
.college .imagesecond {
width: 550px;
height: 900px;
transform: translate(-110px, 500px);
transition: transform 0.5s ease-in-out 0.25s;
border-radius: 8px;
overflow: hidden;
}
.college:hover>.imagesecond {
transform: translate(-110px, -500px);
}
.college:hover>body {
background-color: black
}
.lycee .image {
left: 700px;
top: 500px;
position: absolute
}
.lycee .imagefourth {
width: 537px;
height: 600px;
transform: translate(-160px, 500px);
transition: transform 0.5s ease-in-out 0.2s;
border-radius: 8px;
overflow: hidden;
}
.lycee:hover>.imagefourth {
transform: translate(-160px, -325px);
}
.formations .image {
left: 1250px;
top: 510px;
position: absolute;
}
.formations .imagesixth {
width: 550px;
height: 900px;
transform: translate(-100px, 400px);
transition: transform 0.5s ease-in-out 0.2s;
border-radius: 8px;
overflow: hidden
}
.formations:hover>.imagesixth {
transform: translate(-173px, -600px);
}
body {
background: url("accueil.png") 33em 0% fixed no-repeat;
position: fixed;
background-color: white/* rgb(0,85,170); */
}
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="css.css" />
<title> sainte marie </title>
</head>
<body>
<div class="saintemarie">
<a href="college/collegesaintemarie.html">
<div onmouseOver="mouseOver()" onmouseout="mouseOut()" class="college">
<img class="image imagefirst" src="http://via.placeholder.com/196x175" />
<img class="image imagesecond" src="http://via.placeholder.com/320x440" />
</div>
</a>
<a href="lycee/lyceesaintemarie.html">
<div class="lycee">
<img class="image imagethird" src="http://via.placeholder.com/183x140" />
<img class="image imagefourth" src="http://via.placeholder.com/320x440" />
</div>
</a>
<a href="c&formation/c&fsaintemarie.html">
<div class="formations">
<img class="image imagefifth" src="http://via.placeholder.com/172x153" />
<img class="image imagesixth" src="http://via.placeholder.com/320x440" />
</div>
</a>
</div>
</body>
</html>
答案 2 :(得分:1)
这样的东西?
function mouseOver() {
document.body.style.backgroundColor = "black"
}
function mouseOut() {
document.body.style.backgroundColor = "white"
}
&#13;
.college{
height: 100px;
width: 100px;
border: 1px solid black;
}
.college .image {
left: 100px;
top: 475px;
position: absolute
}
.college:hover .imagefirst {
opacity: 0.2;
}
.college .imagesecond {
width: 550px;
height: 900px;
transform: translate(-110px, 500px);
transition: transform 0.5s ease-in-out 0.25s;
border-radius: 8px;
overflow: hidden;
}
.college:hover>.imagesecond {
transform: translate(-110px, -500px);
}
.college:hover>body {
background-color: black
}
.lycee .image {
left: 700px;
top: 500px;
position: absolute
}
.lycee .imagefourth {
width: 537px;
height: 600px;
transform: translate(-160px, 500px);
transition: transform 0.5s ease-in-out 0.2s;
border-radius: 8px;
overflow: hidden;
}
.lycee:hover>.imagefourth {
transform: translate(-160px, -325px);
}
.formations .image {
left: 1250px;
top: 510px;
position: absolute;
}
.formations .imagesixth {
width: 550px;
height: 900px;
transform: translate(-100px, 400px);
transition: transform 0.5s ease-in-out 0.2s;
border-radius: 8px;
overflow: hidden
}
.formations:hover>.imagesixth {
transform: translate(-173px, -600px);
}
body {
background: url("accueil.png") 33em 0% fixed no-repeat;
position: fixed;
background-color: white/* rgb(0,85,170); */
}
&#13;
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="css.css" />
<title> sainte marie </title>
</head>
<body>
<div class="saintemarie">
<a href="college/collegesaintemarie.html">
<div onmouseover="mouseOver();" onmouseout="mouseOut();" class="college">
<img class="image imagefirst" src="http://via.placeholder.com/196x175" />
<img class="image imagesecond" src="http://via.placeholder.com/320x440" />
</div>
</a>
<a href="lycee/lyceesaintemarie.html">
<div class="lycee">
<img class="image imagethird" src="http://via.placeholder.com/183x140" />
<img class="image imagefourth" src="http://via.placeholder.com/320x440" />
</div>
</a>
<a href="c&formation/c&fsaintemarie.html">
<div class="formations">
<img class="image imagefifth" src="http://via.placeholder.com/172x153" />
<img class="image imagesixth" src="http://via.placeholder.com/320x440" />
</div>
</a>
</div>
</body>
</html>
&#13;
答案 3 :(得分:1)
1)
please see your function name carefully
<div onmouseover="mouseover()" onmouseout="mouseout()" class="college">
'O' should be capital as you defined it mouseOver() and mouseOut();
2)
for using byName you must have tag with that name so for using
document.getElementsByClassName("body").style.backgroundColor = "black";
add name="body" in <body>
for example <body name="body">
或
simply use byTagName atribute like this
document.getElementsByTagName("body")[0].style.backgroundColor = "black";
快乐编码,:-)
答案 4 :(得分:0)
您正在尝试使用正在查找课程的getElementsByClassName
获取代码,因此请使用下面代码段中的getElementsByTagName
或将class="body"
添加到正文代码
function mouseOver() {
document.getElementsByTagName("body")[0].style.backgroundColor = "#000";
}
function mouseOut() {
document.getElementsByTagName("body")[0].style.backgroundColor = "white";
}
.college .image {
left: 100px;
top: 475px;
position: absolute
}
.college{
border:1px solid black;
height: 50px;
width:50px;
}
.college:hover .imagefirst {
opacity: 0.2;
}
.college .imagesecond {
width: 550px;
height: 900px;
transform: translate(-110px, 500px);
transition: transform 0.5s ease-in-out 0.25s;
border-radius: 8px;
overflow: hidden;
}
.college:hover>.imagesecond {
transform: translate(-110px, -500px);
}
.college:hover>body {
background-color: black
}
.lycee .image {
left: 700px;
top: 500px;
position: absolute
}
.lycee .imagefourth {
width: 537px;
height: 600px;
transform: translate(-160px, 500px);
transition: transform 0.5s ease-in-out 0.2s;
border-radius: 8px;
overflow: hidden;
}
.lycee:hover>.imagefourth {
transform: translate(-160px, -325px);
}
.formations .image {
left: 1250px;
top: 510px;
position: absolute;
}
.formations .imagesixth {
width: 550px;
height: 900px;
transform: translate(-100px, 400px);
transition: transform 0.5s ease-in-out 0.2s;
border-radius: 8px;
overflow: hidden
}
.formations:hover>.imagesixth {
transform: translate(-173px, -600px);
}
body {
background: url("accueil.png") 33em 0% fixed no-repeat;
position: fixed;
background-color: white/* rgb(0,85,170); */
}
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="css.css" />
<title> sainte marie </title>
</head>
<body>
<div class="saintemarie">
<a href="college/collegesaintemarie.html">
<div onmouseOver="mouseOver()" onmouseout="mouseOut()" class="college">
<img class="image imagefirst" src="http://via.placeholder.com/196x175" />
<img class="image imagesecond" src="http://via.placeholder.com/320x440" />
</div>
</a>
<a href="lycee/lyceesaintemarie.html">
<div class="lycee">
<img class="image imagethird" src="http://via.placeholder.com/183x140" />
<img class="image imagefourth" src="http://via.placeholder.com/320x440" />
</div>
</a>
<a href="c&formation/c&fsaintemarie.html">
<div class="formations">
<img class="image imagefifth" src="http://via.placeholder.com/172x153" />
<img class="image imagesixth" src="http://via.placeholder.com/320x440" />
</div>
</a>
</div>
</body>
</html>