【发布时间】:2011-06-21 17:15:20
【问题描述】:
<script type="text/javascript">
$(document).ready(function(){
$.cacheImage(['bilder/in_bearbeitung.png'])
var cache = {'': $('.startseite')};
$(window).bind( 'hashchange', function(e) {
var url = $.param.fragment();
if (url == ("telefonmeeting.html") || url == ("vorteile.html") || url == ("faq.html") || url == ("flatrate.html") || url == ("unternehmen.html") || url == ("impressum.html") || url == ("kontakt.html") || url == ("agb.html") || url == ("facebook.html") || url == ("news.html") || url == ("links.html"))
{
$( '.inhalt-container' ).fadeOut();
}
if ( cache[ url ] )
{
cache[ url ].fadeIn();
}
if (url == ("telefonmeeting.html") || url == ("vorteile.html") || url == ("faq.html") || url == ("flatrate.html") || url == ("unternehmen.html") || url == ("impressum.html") || url == ("kontakt.html") || url == ("agb.html") || url == ("facebook.html") || url == ("news.html") || url == ("links.html"))
{
$('html, body').animate({scrollTop:0}, 'fast');
cache[ url ] = $( '<div class="inhalt-"/>' )
.appendTo( '.inhalt-container' )
$( '.inhalt-container' ).load( url , function( data ){
$(this).html( data );
$(this).fadeIn();
});
}
else if (url == ("_1") || url == ("_2"))
{
$('.inhalt-container, .link , .sublink').click(function(event){
//prevent the default action for the click event
event.preventDefault();
//get the full url - like mysitecom/index.htm#home
var full_url = this.href;
//split the url by # and get the anchor target name - home in mysitecom/index.htm#home
var parts = full_url.split("#");
var trgt = parts[1];
//get the top offset of the target anchor
var target_offset = $("#"+trgt).offset();
var target_top = target_offset.top;
//goto that anchor by setting the body scroll top to anchor top
$('html, body').animate({"scrollTop":target_top}, 750);
});
}
})
// Since the event is only triggered when the hash changes, we need to trigger
// the event now, to handle the hash the page may have loaded with.
$(window).trigger( 'hashchange' );
});
</script>
【问题讨论】:
-
你调试过,看看target_top的值是多少?
-
你有一个工作演示,也许是一个 jsfiddle?
-
为我工作:jsfiddle.net/Fj9LY 任何其他可能干扰此的代码或 HTML 标记?你在什么浏览器上测试?
-
我正在 Firefox 4.0 上测试网站
-
你可以在link看到开发页面。第三个链接“Fragen und Antworten”打开了锚点。你想让我做一个 jsFiddle 吗?
标签: jquery scroll jquery-animate anchor