0

我的 sidebar.css :

body
{
  margin: 0;
  padding: 0;
}
#header
{
  background-color: #577481;
  float: left;
  height: 50px;
  width: 100%;
}
#sidebar
{
  background-color: #4ea9d1;
  float: left;
  height: 100%;
  left: -200px;
  position: absolute;
  top: 50px;
  width: 200px;
}
#wrapper
{
  float: left;
  height: 100%;
  width: 100%;
}
#content
{
  background-color: #d6b141;
  float: left;
  height: 500px;
  width: 100%;
}
#sideBarButton
{
  background-image: url('SideBarButton.png');
  background-position: center;
  background-repeat: no-repeat;
  background-size: 70% 70%;
  border-radius: 25px;
  height: 50px;
  margin-left: 30px;
  width: 50px;
}

我的 index.html:

<<!DOCTYPE html>
<html>

  <head>
    <link rel="stylesheet" type="text/css" href="sidebar.css">
    <script>
      function slideIn(element1, element2, element3) {
        var elem = document.getElementById(element1);
        var elem2 = document.getElementById(element2);
        var elem3 = document.getElementById(element3);
        elem.style.transition = "left 0.5s ease-in 0s";
        elem.style.left = "0px";
        elem2.style.transition = "marginLeft 0.5s ease-in 0s";
        elem2.style.marginLeft = "200px";
        elem2.style.opacity = 0.75;
        elem3.style.backgroundImage = 'url(SideBarButtonHover.png)';
        elem3.style.backgroundColor = '#3f545d';
      }

      function slideOut(element1, element2, element3) {
        var elem = document.getElementById(element1);
        var elem2 = document.getElementById(element2);
        var elem3 = document.getElementById(element3);
        elem.style.transition = "left 0.5s ease-out 0s";
        elem.style.left = "-200px";
        elem2.style.transition = "marginLeft 0.5s ease-out 0s";
        elem2.style.marginLeft = "0px";
        elem2.style.opacity = 1;
        elem3.style.backgroundImage = 'url(SideBarButton.png)';
        elem3.style.backgroundColor = '#577481';
      }
    </script>
  </head>

  <body>
    <div id="header">
      <div id="sideBarButton" onMouseOver="slideIn('sidebar', 'content', 'sideBarButton');" onMouseOut="slideOut('sidebar', 'content', 'sideBarButton');"></div>
    </div>
    <div id="wrapper">
      <div id="sidebar">
        <ul>
          <li><a href="#">Hallo</a>
          </li>
        </ul>
      </div>
      <div id="content">Ich bin der Content</div>
    </div>
  </body>

</html>

带有第一个元素(侧边栏)的过渡有效,但不适用于第二个元素(内容)。当您将鼠标悬停在侧边栏按钮上时,它应该像这样工作,侧边栏会出来并且内容会同时向右移动。我的问题是为什么内容转换不起作用?谢谢你们的帮助!

带有第一个元素(侧边栏)的过渡有效,但不适用于第二个元素(内容)。当您将鼠标悬停在侧边栏按钮上时,它应该像这样工作,侧边栏会出来并且内容会同时向右移动。我的问题是为什么内容转换不起作用?

谢谢你们的帮助!

4

1 回答 1

2

在 javascript 中设置样式值时,您的样式名称应该是驼峰式命名的,但如果在其中设置带有 CSS 属性的值,则应该使用 CSS 属性语法。正确的是:

elem2.style.transition = "margin-left 0.5s ease-in 0s";
elem2.style.marginLeft = "200px";
于 2013-11-12T20:20:20.933 回答