Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用滚动添加活动类,但不使用href id?_Javascript_Jquery - Fatal编程技术网

Javascript 如何使用滚动添加活动类,但不使用href id?

Javascript 如何使用滚动添加活动类,但不使用href id?,javascript,jquery,Javascript,Jquery,如果不使用 这是js <script type="text/javascript"> $(".page1").click(function() { $('html, body').animate({ scrollTop: $("#home").offset().top }, 1000); }); $(".page2").click(function() { $('html, bo

如果不使用

  • 这是js

    <script type="text/javascript">
        $(".page1").click(function() {
            $('html, body').animate({
                scrollTop: $("#home").offset().top
            }, 1000);
        });
        $(".page2").click(function() {
            $('html, body').animate({
                scrollTop: $("#blog").offset().top
            }, 1000);
        });
    
        $(".page3").click(function() {
            $('html, body').animate({
                scrollTop: $("#about").offset().top
            }, 1000);
        });
    
        $(".page4").click(function() {
            $('html, body').animate({
                scrollTop: $("#contact").offset().top
            }, 1000);
        });
    </script>
    <!-- point active carousel -->
    <script type="text/javascript">
        $('li a').click(function(e) {
            e.preventDefault();
            $('a').removeClass('active');
            $(this).addClass('active');
        });
    </script>
    
    
    $(“.page1”)。单击(函数(){
    $('html,body')。设置动画({
    scrollTop:$(“#home”).offset().top
    }, 1000);
    });
    $(“.page2”)。单击(函数(){
    $('html,body')。设置动画({
    scrollTop:$(“#blog”).offset().top
    }, 1000);
    });
    $(“.page3”)。单击(函数(){
    $('html,body')。设置动画({
    scrollTop:$(“#关于”).offset().top
    }, 1000);
    });
    $(“.page4”)。单击(函数(){
    $('html,body')。设置动画({
    scrollTop:$(“#联系人”).offset().top
    }, 1000);
    });
    $('li a')。单击(函数(e){
    e、 预防默认值();
    $('a').removeClass('active');
    $(this.addClass('active');
    });
    
    试试这样的方法

    $('.navbar>a').click(function(e){
      //preventing the default href
      e.preventDefault();
      $(this).addClass(active)
      var href = $(this).attr('href');
      $(document).scrollTo(href);
    })
    

    给你一个解决方案

    $('.单击a')。单击(函数(e){
    e、 预防默认值();
    $(this).parent().addClass('active')。同胞('li')。removeClass('active');
    var id=$(this.data('href');
    $('html,body')。设置动画({
    scrollTop:$(“#”+id).offset().top
    }, 1000);
    });
    
    .active{
    字体大小:粗体;
    }
    
    

    yes,使用event.preventDefault()请创建一个。我正在尝试触发活动类的on-scroll,而不是on-clicktry($document)。on('scroll',function(){})感谢您的回复。我正在尝试查找添加活动类的滚动条为什么为负数?请解释。您的代码供我单击选项卡,并将被视为已激活。我想做的只是滚动到特定的div,选项卡就会被激活。就像一个旋转木马一样slideshow@JohnDoe给你答案
    $('.navbar>a').click(function(e){
      //preventing the default href
      e.preventDefault();
      $(this).addClass(active)
      var href = $(this).attr('href');
      $(document).scrollTo(href);
    })