2017-05-18 11 views
0

私のコードは次のとおりです。ここでjqueryのスクロールは、スクリプトファイルからイベントを聞くことができない

index.htmlを

$(document).ready(function() { 
 
    $(document).on("scroll", onScroll); 
 
    alert('i am called'); 
 

 
    //smoothscroll 
 
    $('a[href^="#"]').on('click', function(e) { 
 
     e.preventDefault(); 
 
     $(document).off("scroll"); 
 
     $('a').each(function() { 
 
      $(this).removeClass('active'); 
 
     }) 
 
     $(this).addClass('active'); 
 

 
     var target = this.hash, 
 
      menu = target; 
 
     $target = $(target); 
 
     $('html, body').stop().animate({ 
 
      'scrollTop': $target.offset().top + 2 
 
     }, 500, 'swing', function() { 
 
      window.location.hash = target; 
 
      $(document).on("scroll", onScroll); 
 
     }); 
 
    }); 
 
}); 
 

 
function onScroll(event) { 
 
    alert('i am clickked'); 
 
    var scrollPos = $(document).scrollTop(); 
 
    $('#q-nav a').each(function() { 
 
     var currLink = $(this); 
 
     var refElement = $(currLink.attr("href")); 
 
     if (refElement.position().top <= scrollPos && refElement.position().top + refElement.height() > scrollPos) { 
 
      $('#q-nav ul li a').removeClass("active"); 
 
      currLink.addClass("active"); 
 
     } else { 
 
      currLink.removeClass("active"); 
 
     } 
 
    }); 
 
}
html, 
 
body { 
 
    margin: 0; 
 
    padding: 0; 
 
} 
 
    
 
html { 
 
    font-size: 14px !important; 
 
} 
 
    
 
body { 
 
    font-family: "Open Sans"; 
 
    width: 100%; 
 
    height: 100%; 
 
    overflow-x: hidden; 
 
    overflow-y: scroll; 
 
} 
 
    
 
.container { 
 
    padding: 0; 
 
    width: 100vw; 
 
    height: 100vh; 
 
    background-color: #ffffff; 
 
} 
 
    
 
#q-nav { 
 
    width: 100%; 
 
    position: absolute; 
 
    top: 0; 
 
    padding-top: 3rem; 
 
    color: #ffffff; 
 
} 
 
    
 
#q-nav ul { 
 
    display: flex; 
 
    flex-direction: row; 
 
    justify-content: flex-end; 
 
} 
 
    
 
#q-nav .q-nav-about { 
 
    position: relative; 
 
    color: #ffffff; 
 
    text-decoration: none; 
 
    font-weight: bold; 
 
    padding: 0rem 2rem 0 2rem; 
 
} 
 
    
 
#q-nav ul li { 
 
    list-style: none; 
 
} 
 
    
 
#q-nav .q-nav-work { 
 
    position: relative; 
 
    color: #ffffff; 
 
    text-decoration: none; 
 
    font-weight: bold; 
 
    padding: 0rem 2rem 0 2rem; 
 
} 
 
    
 
#q-nav .q-nav-contact { 
 
    position: relative; 
 
    font-weight: bold; 
 
    color: #ffffff; 
 
    text-decoration: none; 
 
    padding: 0rem 8rem 0 2rem; 
 
} 
 
    
 
.q-intro-text { 
 
    height: 100%; 
 
    position: absolute; 
 
    top: 0; 
 
    flex-direction: column; 
 
    justify-content: center; 
 
    display: flex; 
 
} 
 
    
 
.q-intro-text .q-mobile-app-text { 
 
    margin: 0 0 0 10rem; 
 
    color: #ffffff; 
 
} 
 
    
 
#about-us { 
 
    width: 100%; 
 
    height: 100%; 
 
    background-color: green; 
 
} 
 
    
 
#work { 
 
    width: 100%; 
 
    height: 100%; 
 
    background-color: purple; 
 
} 
 
    
 
#contact-us { 
 
    width: 100%; 
 
    height: 100%; 
 
    background-color: blue; 
 
}
<!DOCTYPE html> 
 
<html lang="en"> 
 

 
<head> 
 
    <meta charset="utf-8"> 
 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> 
 
    <link rel="stylesheet" type="text/css" href="//fonts.googleapis.com/css?family=Open+Sans" /> 
 
    <link rel="stylesheet" href="../test/css/index.css"> 
 
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
 
</head> 
 

 
<body> 
 
    <div class="container"> 
 
     <div id="myCarousel" class="carousel slide" data-ride="carousel"> 
 
      <ol class="carousel-indicators"> 
 
       <li data-target="#myCarousel" data-slide-to="0" class="active"></li> 
 
       <li data-target="#myCarousel" data-slide-to="1"></li> 
 
       <li data-target="#myCarousel" data-slide-to="2"></li> 
 
      </ol> 
 

 
      <!-- Wrapper for slides --> 
 
      <div class="carousel-inner"> 
 
       <div class="item active"> 
 
        <img src="../test/images/slider1.png" alt="Los Angeles" style=" 
 
        height:100vh;width:100vw;"> 
 
       </div> 
 

 
       <div class="item"> 
 
        <img src="../test/images/slider-2.png" alt="Chicago" style=" 
 
        height:100vh;width:100vw;"> 
 
       </div> 
 

 
       <div class="item"> 
 
        <img src="../test/images/slider-3.png" alt="New york" style=" 
 
        height:100vh;width:100vw;"> 
 
       </div> 
 
      </div> 
 
     </div> 
 
    </div> 
 

 
    <div id="q-nav"> 
 
     <ul> 
 
      <li><a class="active q-nav-about" href="#about-us">ABOUT US</a> 
 
      </li> 
 
      <li><a class="q-nav-work" href="#work">WORK</a> 
 
      </li> 
 
      <li><a class="q-nav-contact" href="#contact-us">CONTACT US</a> 
 
      </li> 
 
      </li> 
 
     </ul> 
 

 
    </div> 
 

 
    <div class="q-intro-text"> 
 
     <div class="q-mobile-app-text">MOBILE APP TEXT<br/>All the work and no play makes jack a dull boy! 
 
      <br/> Join Libr8 cricket league. Unleash the <br/> sportsman in you</div> 
 
    </div> 
 

 
    <div id="#about-us"> 
 
     <img src="../test/images/slider-3.png" alt="New york" style=" 
 
        height:100vh;width:100vw;"> 
 
    </div> 
 
    <div id="#work"> 
 
     <img src="../test/images/slider-2.png" alt="Chicago" style=" 
 
        height:100vh;width:100vw;"> 
 
    </div> 
 
    <div id="#contact-us"> 
 
     <img src="../test/images/slider1.png" alt="Los Angeles" style=" 
 
        height:100vh;width:100vw;"> 
 
    </div> 
 
    <script src="../test/scripts/scrolling.js"></script> 
 

 
</body> 
 

 
</html>

、私は置くことをしようとしていますhtmlファイル内のコードでさえ、何らかの形でスクロールダウンまたはa-hrefリンクをクリックすると、その特定のdivにスクロールできません。では、jqueryファイルやナビゲーションメニューに問題はありますか?

答えて

1

divタグのidから#を削除します。

<div id="about-us"> 
<img src="../test/images/slider-3.png" alt="New york" style=" height:100vh;width:100vw;"> 
</div> 
<div id="work"> 
<img src="../test/images/slider-2.png" alt="Chicago" style=" height:100vh;width:100vw;"> 
</div> 
<div id="contact-us"> 
<img src="../test/images/slider1.png" alt="Los Angeles" style=" height:100vh;width:100vw;"> 
</div> 
関連する問題