我的基于CSS的页面存在问题。 我想将鼠标上的背景图像更改为两个div,并在单击时指向另一个页面。 我有3个问题:
谢谢!
* {
padding: 0;
margin: 0;
}
html,
body {
height: 100%;
text-align: center;
}
@media screen and (orientation:landscape) {
.container {
position: absolute;
display: flex;
width: 75vmin;
height: 100vmin;
top: 50%;
left: 50%;
transform: translate3d(-50%, -50%, 0);
}
.div1,
.div2 {
display: flex;
align-items: center;
justify-content: center;
flex: 1;
width: 32.5vmin;
height: 100vmin;
background-color: #ddd;
}
}
@media screen and (orientation:portrait) {
.container {
position: absolute;
display: flex;
width: 100vmin;
height: 133vmin;
top: 50%;
left: 50%;
transform: translate3d(-50%, -50%, 0);
}
.div1,
.div2 {
display: flex;
align-items: center;
justify-content: center;
flex: 1;
width: 50vmin;
height: 133vmin;
background-color: hsla(0, 0%, 0%, 0.1);
}
}
.background1,
.background2 {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
display: none;
z-index: -1;
}
.background1 {
background: url("http://i.imgur.com/zFYHM67.jpg");
background-repeat: no-repeat;
background-size: cover;
}
.background2 {
background: url("http://i.imgur.com/nYKEFNF.jpg");
background-repeat: no-repeat;
background-size: cover;
}
.div1:hover~.background1 {
display: flex;
}
.div2:hover~.background2 {
display: flex;
}

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<div class="div1">Div 1</div>
<div class="background1"></div>
<div class="div2">Div 2</div>
<div class="background2"></div>
</div>
</body>
</html>
&#13;
答案 0 :(得分:1)
我对您的代码(主要是css)进行了一些更改,请参阅下面的代码段:
问题1的 :您使用的是@media screen and (orientation:portrait)
,因此内部样式仅适用于肖像
:我使用opacity
和transition
来获得动画效果
:我刚将div
代码更改为a
代码
* {
padding: 0;
margin: 0;
}
html,
body {
height: 100%;
text-align: center;
}
@media screen and (orientation:landscape) {
.container {
position: absolute;
display: flex;
width: 75vmin;
height: 100vmin;
top: 50%;
left: 50%;
transform: translate3d(-50%, -50%, 0);
}
.div1,
.div2 {
display: flex;
align-items: center;
justify-content: center;
flex: 1;
width: 32.5vmin;
height: 100vmin;
background-color: #ddd;
}
}
.container {
position: absolute;
display: flex;
width: 100vmin;
height: 100vmin;
top: 50%;
left: 50%;
transform: translate3d(-50%, -50%, 0);
}
.div1,
.div2 {
display: flex;
align-items: center;
justify-content: center;
flex: 1;
width: 50vmin;
height: 100vmin;
background-color: hsla(0, 0%, 0%, 0.1);
}
.background1,
.background2 {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
opacity: 0;
z-index: -1;
transition: all 1s;
}
.background1 {
background: url("https://www.w3schools.com/css/img_fjords.jpg");
background-repeat: no-repeat;
background-size: cover;
}
.background2 {
background: url("https://www.w3schools.com/howto/img_mountains.jpg");
background-repeat: no-repeat;
background-size: cover;
}
.div1:hover~.background1 {
display: flex;
opacity: 1;
}
.div2:hover~.background2 {
display: flex;
opacity: 1;
}
&#13;
<div class="container">
<a href="#" class="div1">Div 1</a>
<div class="background1"></div>
<a href="#" class="div2">Div 2</a>
<div class="background2"></div>
</div>
&#13;