【问题标题】:Problem with section show and hide with javascript code not work使用 javascript 代码显示和隐藏部分的问题不起作用
【发布时间】:2018-10-22 19:03:34
【问题描述】:

大家,我的 javascript 和 HTML 部分有问题。

我需要点击向下箭头才能从第一个更改为另一个等等。不幸的是,代码没有反应,所以我真的不知道如何解决它。

为了更好的概览而缩短的代码。

主要问题出在 javascript

HTML

HTML 足够好,可以更好地定位。

  <section> <div style="width:40px; height:40px; background-color:black;" onclick="bottomscroll()"> </section>
  <section > <h1>text</h1></section>

  <section> <h1>text</h1></section>

  <section> <h1>text</h1></section>

  <section> <h1>text</h1></section>

CSS

我还缩短了 CSS 以获得更好的概览

.display-block { display:block; }
.display-none { display:none; } 

#fullpage { overflow: hidden; margin: 0; padding: 0;

}

#fullpage section { min-height: 100%; }

#fullpage section h4 { margin: 0; padding: 0; }

#fullpage section:nth-child(1)
{
   color:black;
    background:url(/img/fabian-grohs-597395-unsplash.jpg);
    background-repeat: no-repeat;
    background-size: cover;
    background-position: center;
    height: 100vh;
}

#fullpage section:nth-child(1) h1
{
    margin: 0;
    padding: 0;
    color:whitesmoke;
    text-align: center;
    padding: 70px;
    font-family: 'Montserrat', sans-serif;
    font-size:40px;
}




#fullpage section:nth-child(1) p
{
    text-align: center;
    color:white;
    font-size:18px;
    width: 70%;
    margin:0 auto;
    font-family: 'Inconsolata', monospace;
    position: relative;
}

#fullpage section:nth-child(2)
{
    color:#333;
    background: white;
    min-height: 100vh;
}


#fullpage section:nth-child(2) h1
{
    margin: 0;
    padding: 0;
    color:#333;
    text-align: center;
    padding: 20px 0px;
    font-family: 'Montserrat', sans-serif;
}

#fullpage section:nth-child(3)
{
    color:black;
    background: green;
    min-height: 100vh;
}
#fullpage section:nth-child(4)
{
    color:black;
    background: green;
    min-height: 100vh;
}

#fullpage section:nth-child(5)
{
    color:black;
    background: green;
    min-height: 100vh;
}

Javascript

主要问题出在 javascript

var page = 1;

function topscroll()
{
        if(page != 1)
        {
        page -= 1;    
        var page = document.querySelector("#fullpage section:nth-child("+page")");
            for(i = 0; i < 5; i++)
            {
            var pagehidden = document.querySelector("#fullpage section:nth-child("+i")"); 
            pagehidden.classList.add("display-none");   
            pagehidden.classList.remove("display-block");     
            }
        page.classList.add("display-block");    
        }
}

function bottomscroll()
{
        if(page != 5)
        {
        page += 1;    
        var page = document.querySelector("#fullpage section:nth-child("+page")");
            for(i = 0; i < 5; i++)
            {
            var pagehidden = document.querySelector("#fullpage section:nth-child("+i")"); 
            pagehidden.classList.add("display-none");  
            pagehidden.classList.remove("display-block");   
            }
        page.classList.add("display-block");    
        }
}

var page = 1;

function topscroll()
{
        if(page != 1)
        {
        page -= 1;    
        var page = document.querySelector("#fullpage section:nth-child("+page")");
            for(i = 0; i < 5; i++)
            {
            var pagehidden = document.querySelector("#fullpage section:nth-child("+i")"); 
            pagehidden.classList.add("display-none");   
            pagehidden.classList.remove("display-block");     
            }
        page.classList.add("display-block");    
        }
}

function bottomscroll()
{
        if(page != 5)
        {
        page += 1;    
        var page = document.querySelector("#fullpage section:nth-child("+page")");
            for(i = 0; i < 5; i++)
            {
            var pagehidden = document.querySelector("#fullpage section:nth-child("+i")"); 
            pagehidden.classList.add("display-none");  
            pagehidden.classList.remove("display-block");   
            }
        page.classList.add("display-block");    
        }
}
.display-block { display:block; }
.display-none { display:none; } 

#fullpage { overflow: hidden; margin: 0; padding: 0;

}

#fullpage section { min-height: 100%; }

#fullpage section h4 { margin: 0; padding: 0; }

#fullpage section:nth-child(1)
{
   color:black;
    background:url(/img/fabian-grohs-597395-unsplash.jpg);
    background-repeat: no-repeat;
    background-size: cover;
    background-position: center;
    height: 100vh;
}

#fullpage section:nth-child(1) h1
{
    margin: 0;
    padding: 0;
    color:whitesmoke;
    text-align: center;
    padding: 70px;
    font-family: 'Montserrat', sans-serif;
    font-size:40px;
}




#fullpage section:nth-child(1) p
{
    text-align: center;
    color:white;
    font-size:18px;
    width: 70%;
    margin:0 auto;
    font-family: 'Inconsolata', monospace;
    position: relative;
}

#fullpage section:nth-child(2)
{
    color:#333;
    background: white;
    min-height: 100vh;
}


#fullpage section:nth-child(2) h1
{
    margin: 0;
    padding: 0;
    color:#333;
    text-align: center;
    padding: 20px 0px;
    font-family: 'Montserrat', sans-serif;
}

#fullpage section:nth-child(3)
{
    color:black;
    background: green;
    min-height: 100vh;
}
#fullpage section:nth-child(4)
{
    color:black;
    background: green;
    min-height: 100vh;
}

#fullpage section:nth-child(5)
{
    color:black;
    background: green;
    min-height: 100vh;
}
  <section> <div style="width:40px; height:40px; background-color:black;" onclick="bottomscroll()"> </section>
  <section><h1>text</h1></section>

  <section><h1>text</h1></section>

  <section><h1>text</h1></section>

  <section><h1>text</h1></section>

【问题讨论】:

    标签: javascript html css sections


    【解决方案1】:

    您的问题是,每次您将元素循环到 add display-none 类然后 remove display-block 然后在循环之后添加 @ 987654323@ 到需要的元素,而不删除循环内添加的display-none

    代码应该是这样的:

    function bottomscroll()
    {
            ... 
            page.classList.remove("display-none"); /*added this*/
            page.classList.add("display-block");    
            ...
    }
    

    topscroll()的逻辑相同

    这是一个完整的工作代码,只有一个功能(纠正其他错误后):

    var nb = 1;
    
    
    function bottomscroll() {
      if (nb != 4) {
        nb += 1;
        var page = document.querySelector("#fullpage section:nth-child(" + nb + ")");
        for (var i = 1; i < 4; i++) {
          var pagehidden = document.querySelector("#fullpage section:nth-child(" + i + ")");
          pagehidden.classList.add("display-none");
          pagehidden.classList.remove("display-block");
        }
        page.classList.remove("display-none");
        page.classList.add("display-block");
      }
    }
    .display-block {
      display: block;
    }
    
    .display-none {
      display: none;
    }
    
    #fullpage {
      overflow: hidden;
      margin: 0;
      padding: 0;
    }
    
    #fullpage section {
      min-height: 100%;
    }
    
    #fullpage section h4 {
      margin: 0;
      padding: 0;
    }
    
    #fullpage section:nth-child(1) {
      color: black;
      background: url(/img/fabian-grohs-597395-unsplash.jpg);
      background-repeat: no-repeat;
      background-size: cover;
      background-position: center;
      height: 100vh;
    }
    
    #fullpage section:nth-child(1) h1 {
      margin: 0;
      padding: 0;
      color: whitesmoke;
      text-align: center;
      padding: 70px;
      font-family: 'Montserrat', sans-serif;
      font-size: 40px;
    }
    
    #fullpage section:nth-child(1) p {
      text-align: center;
      color: white;
      font-size: 18px;
      width: 70%;
      margin: 0 auto;
      font-family: 'Inconsolata', monospace;
      position: relative;
    }
    
    #fullpage section:nth-child(2) {
      color: #333;
      background: white;
      min-height: 100vh;
    }
    
    #fullpage section:nth-child(2) h1 {
      margin: 0;
      padding: 0;
      color: #333;
      text-align: center;
      padding: 20px 0px;
      font-family: 'Montserrat', sans-serif;
    }
    
    #fullpage section:nth-child(3) {
      color: black;
      background: green;
      min-height: 100vh;
    }
    
    #fullpage section:nth-child(4) {
      color: black;
      background: green;
      min-height: 100vh;
    }
    
    #fullpage section:nth-child(5) {
      color: black;
      background: blue;
      min-height: 100vh;
    }
    <div style="width:40px; height:40px; background-color:black;" onclick="bottomscroll()"> </div>
    <div id="fullpage">
      
      <section>
        <h1>text1</h1>
      </section>
    
      <section>
        <h1>text2</h1>
      </section>
    
      <section>
        <h1>text3</h1>
      </section>
    
      <section>
        <h1>text4</h1>
      </section>
    </div>

    【讨论】:

    • document.querySelector("#fullpage section:nth-child("+page")"); 无效。你/他在变量 pagei 后面缺少一个加号
    • @SølveTornøe 他在他的代码中遗漏了很多东西。我在一个有效的 sn-p 中更正了他的代码
    • 很好!我没想到你会编辑你的答案。
    • 我不知道在网络上(实时预览括号)代码不起作用。
    猜你喜欢
    • 2014-07-02
    • 1970-01-01
    • 2014-12-27
    • 1970-01-01
    • 2012-04-15
    • 2012-03-11
    • 2016-08-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多