2013-05-02 68 views
0

我用下面作任何锚与类.jump-link滚动到它引用在href属性ID:jQuery的滚动到ID脚本只向下滚动

// Scroll to ID 
$('.jump-link').click(function() { 
    var el = $(this).attr('href'), 
     elWrapped = $(el); 
    scrollToId(elWrapped, 40); 
    return false; 
}); 
function scrollToId(element, navheight) { 
    var offset = element.offset(), 
     offsetTop = offset.top, 
     totalScroll = offsetTop - navheight; 
    $('body,html').animate({ 
     scrollTop: totalScroll 
    }, 500); 
} 

但不幸的是它似乎只工作滚动下载该页面。我还希望脚本能够滚动页面。

+0

你说,这是该网页总是向下滚动,不论ID提及? – dreamweiver 2013-05-02 11:13:48

+2

[在此处双击](http://jsfiddle.net/447j6/)。您需要进一步隔离您的问题,因为问题根据您迄今给出的内容无法重现。 – 2013-05-02 11:13:49

+0

当我找出问题时([Fiddle](http://jsfiddle.net/alecrust/MY7tb/)),它似乎按预期工作。很奇怪。 – AlecRust 2013-05-02 11:29:15

回答

0

试试这个..这将是工作。

$(".jump-link").click(function(){ 
var id = $(this).attr('href'); 
     $('html, body').animate({scrollTop: $("#"+id).offset().top}, 2000); 
}); 
+0

用这个脚本代替并不幸运。 – AlecRust 2013-05-02 11:31:46

+0

当您的代码的破碎部分未包含在您的问题中时,您很难指出需要更改的内容。 – 2013-05-02 11:32:40

+0

我不知道为什么不这样做,但使用这个脚本它为我工作得很好。 – 2013-05-02 11:33:48

0

这里是代码试试这个

$(function() { 
    $('ul.nav a').bind('click',function(event){ 
     var $anchor = $(this); 
     $('html, body').stop().animate({ 
      scrollTop: $($anchor.attr('href')).offset().top 
     }, 1200); 
     event.preventDefault(); 
    }); 
}); 
+0

不幸的是,用这个脚本替换并不奏效。 – AlecRust 2013-05-02 11:30:49

+0

你实现你的'id'或'class',但是你使用show me小提琴来解决它会定义的问题 – 2013-05-02 11:33:04

0
<script type="text/javascript"> 
     jQuery(document).ready(function($) { 
      $(".scroll").click(function(event){  
       event.preventDefault(); 
       $('html,body').animate({scrollTop:$(this.hash).offset().top},1200); 
      }); 
     }); 
    </script>