用户滚动时调整标题大小

时间:2016-03-26 19:01:08

标签: javascript html header resize onscroll

这是我的代码,在滚动时重新调整标题大小,但它不起作用。没有将标题设置为固定大小。但是标题大小不变,我使用Javscript和css来这样做但是有些人可以帮助我摆脱这个错误。我在这个结构,所以请一些人可以帮助我。 提前致谢

     <!DOCTYPE html>

       <html lang="en">


        <head>


       <title>Request Us Your Requirment</title>




          <link rel="stylesheet" href="css/style.css" />

         <script src="js/classie.js">
         </script>

          <script>

          function init() {

          window.addEventListener('scroll', function(e){

            var distanceY = window.pageYOffset ||  document.documentElement.scrollTop,
            shrinkOn = 300,
            header = document.querySelector("header");
               if (distanceY > shrinkOn) {

               classie.add(header,"smaller");

                  } 
                 else {

                if (classie.has(header,"smaller")) {

                 classie.remove(header,"smaller");

                     }

                         }

                        });

                        }

                window.onload = init();

                </script>

              </head>




                <body>


                 <div id="wrapper">


                  <header>

                <div class="container clearfix">

                 <h1 id="logo">
                  My Site
                 </h1>

                   <nav>

                   <a href="">Services</a>

                <a href="">About Us</a>

                <a href="">Contact Us</a>

                  </nav>

                 </div>

                  </header>


                  <div id="main">

                  <div id="content">

                <section>

                <div class="container">

                  <h1>Request Us Your Requirement</h1>

                        <p>Building a website is a time consuming process.But not now Just get your professional website by contsct us</p>

                <p>We will Provide the best web sites with less cost and within the time.</p>

                      <p>
                  <a href="http://www.facebook.com/vinod.dirishala">Follow Us On FB</a><br>

                     <a href="http://www.twitter.com/vinod.dirishala">Follows Us On Twitter</a>
            </p>

                    </div>

                 </section>

                  <section class="color">

               <div class="container">

             <h1>Why My Site!</h1>

            <p>We will provide Best Quality Webstes</p>

            <p>Low Cost</p>

             <p> How to contact Us</p>

              </div>

             </section>

             </div>

               </div> 



                <footer>

                 <div id="info-bar">

                     <div class="container clearfix">

                   <span class="all-tutorials">

                     </div>

                         </div>

                      </footer>




                         </div>



                         </body>

                            </html>

1 个答案:

答案 0 :(得分:0)

检查这个小提琴:https://jsfiddle.net/b39gx6pa/2/

将JavaScript替换为:

function init() {      
     window.addEventListener('scroll', function(e){    
         var distanceY = window.pageYOffset || document.documentElement.scrollTop,shrinkOn = 300,header = document.querySelector("header");

         if (distanceY > header.clientHeight/3) {             
            classie.add(header,"smaller");          
         }else {           
            if (classie.has(header,"smaller")) {
               classie.remove(header,"smaller");
            }
         }   
     });
}

window.onload = init();