过渡效果不会将过渡图像移除吗?

时间:2019-01-12 17:26:16

标签: javascript html css

我目前正在用Html创建我的第一个网站,而javascript作为前端将排在第一位。

我正在尝试创建一个折叠边栏,当我单击顶部导航右侧的按钮时会出现该折叠边栏,但是过渡效果有问题,因为它将所有内容推到右侧,但在这样一来,图片就不会显示出来,并且顶部导航不会覆盖顶部,而是位于页面顶部下方,该如何解决?

我拿走了<div id="main">,然后重新显示了背景图片,并且顶部导航栏覆盖了页面顶部,而顶部导航栏应位于顶部导航栏,但是侧边栏现在覆盖了页面的一部分,而不是将其向右移动。

function openNav() {
  document.getElementById("mySidebar").style.width = "250px";
  document.getElementById("main").style.marginLeft = "250px";
  document.body.style.backgroundColor = "rgba(0,0,0,0.4)";
}

function closeNav() {
  document.getElementById("mySidebar").style.width = "0";
  document.getElementById("main").style.marginLeft = "0";
  document.body.style.backgroundColor = "white";
}
#main {
  transition: margin-left .5s;
  padding: 20px;
}

.bgimg_1 {
  position: relative;
  opacity: 1.0;
  background-attachment: fixed;
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
}
.sidebar{
 backfround-color: red;
   }
   .bgimg_1{
 background-image: 
 href("https://cdn.athemes.com/wp-content/uploads/Original-JPG-Image.jpg");
 min-height: 100%;
}
 
<div id="main">
  <div class="topnav">
    <a href="Reids Hub">Reids Hub</a>
    <button class="openbtn" class="tips" onclick="openNav()">&#9776;</button>
  </div>

  <div id="mySidebar" class="sidebar">
    <a href="JavaScript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
    <a href="#">What started all of this.</a>
    <a href="#">About me.</a>
    <a href="#">Projects.</a>
    <a href="#">Contacts.</a>
  </div>

  <div class="bgimg_1">
    <div class="caption">
      <span class="border">SCROLL DOWN</span>
    </div>
  </div>

</div>

我希望一切都能正常进行,但是我也希望图像过渡到页面的其余部分,实际上图像消失了。

1 个答案:

答案 0 :(得分:0)

语法无效

background-image的正确语法是:

  

background-image: URL (http://host.top/path/to/img.jpg);


布局

此外,如果您希望背景覆盖整个视口(屏幕上的花哨单词),请将其放置在父元素中,通常为body。如果您打算这样做,请删除.Bg_img1。整个东西。将以下样式添加到body(请参见演示1 ):

body {
  font-family: "Lato", sans-serif;
  background-image: url(https://3jbq2ynuxa-flywheel.netdna-ssl.com/wp-content/uploads/2018/07/pasted-image-0-19.png);
  background-attachment: fixed;
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
}

如果您希望将background-image保留在右侧,则可以将body而不是#main添加到#main { background-image: url(https://3jbq2ynuxa-flywheel.netdna-ssl.com/wp-content/uploads/2018/07/pasted-image-0-19.png); background-position: center; background-repeat: no-repeat; background-size: cover; transition: margin-left .5s; padding: 16px; color: #fff; min-height: 100vh; } (请参见演示2 ):< / p>

function openNav() {
  document.getElementById("mySidenav").style.width = "250px";
  document.getElementById("main").style.marginLeft = "250px";
}

function closeNav() {
  document.getElementById("mySidenav").style.width = "0";
  document.getElementById("main").style.marginLeft = "0";
}

演示1

body {
  font-family: "Lato", sans-serif;
  background-image: url(https://3jbq2ynuxa-flywheel.netdna-ssl.com/wp-content/uploads/2018/07/pasted-image-0-19.png);
  background-attachment: fixed;
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
}

.sidenav {
  height: 100%;
  width: 0;
  position: fixed;
  z-index: 1;
  top: 0;
  left: 0;
  background-color: #111;
  overflow-x: hidden;
  transition: 0.5s;
  padding-top: 60px;
}

.sidenav a {
  padding: 8px 8px 8px 32px;
  text-decoration: none;
  font-size: 25px;
  color: #818181;
  display: block;
  transition: 0.3s;
}

.sidenav a:hover {
  color: #f1f1f1;
}

.sidenav .closebtn {
  position: absolute;
  top: 0;
  right: 25px;
  font-size: 36px;
  margin-left: 50px;
}

#main {
  transition: margin-left .5s;
  padding: 16px;
  color: #fff;
}

@media screen and (max-height: 450px) {
  .sidenav {
    padding-top: 15px;
  }
  .sidenav a {
    font-size: 18px;
  }
}
<div id="mySidenav" class="sidenav">
  <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
  <a href="#">About</a>
  <a href="#">Services</a>
  <a href="#">Clients</a>
  <a href="#">Contact</a>
</div>

<div id="main">
  <h2>Sidenav Push Example</h2>
  <p>Click on the element below to open the side navigation menu, and push this content to the right.</p>
  <span style="font-size:30px;cursor:pointer" onclick="openNav()">&#9776; open</span>
</div>
function openNav() {
  document.getElementById("mySidenav").style.width = "250px";
  document.getElementById("main").style.marginLeft = "250px";
}

function closeNav() {
  document.getElementById("mySidenav").style.width = "0";
  document.getElementById("main").style.marginLeft = "0";
}

演示2

body {
   font-family: "Lato", sans-serif;
}

.sidenav {
  height: 100%;
  width: 0;
  position: fixed;
  z-index: 1;
  top: 0;
  left: 0;
  background-color: #111;
  overflow-x: hidden;
  transition: 0.5s;
  padding-top: 60px;
}

.sidenav a {
  padding: 8px 8px 8px 32px;
  text-decoration: none;
  font-size: 25px;
  color: #818181;
  display: block;
  transition: 0.3s;
}

.sidenav a:hover {
  color: #f1f1f1;
}

.sidenav .closebtn {
  position: absolute;
  top: 0;
  right: 25px;
  font-size: 36px;
  margin-left: 50px;
}

#main { 
  background-image: url(https://3jbq2ynuxa-flywheel.netdna-ssl.com/wp-content/uploads/2018/07/pasted-image-0-19.png);
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
  transition: margin-left .5s;
  padding: 16px;
  color: #fff;
  min-height: 100vh;
}

@media screen and (max-height: 450px) {
  .sidenav {
    padding-top: 15px;
  }
  .sidenav a {
    font-size: 18px;
  }
}
<div id="mySidenav" class="sidenav">
  <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
  <a href="#">About</a>
  <a href="#">Services</a>
  <a href="#">Clients</a>
  <a href="#">Contact</a>
</div>

<div id="main">
  <h2>Sidenav Push Example</h2>
  <p>Click on the element below to open the side navigation menu, and push this content to the right.</p>
  <span style="font-size:30px;cursor:pointer" onclick="openNav()">&#9776; open</span>
</div>
Login