2013-01-21 87 views
0

我有一个“滑出”div,我通过CSS定位隐藏/显示,并使用jquery切换设置div的隐藏/显示状态的类。如何使用Jquery执行指定操作后的默认链接操作?

这一切都很好。我现在要做的是让页面上更远的任何链接改变类以显示div,然后跳转到该页面。

这是我有:

$('a[href="#request-trial"]').click(function() { 
    $("#request-trial").toggleClass("on off"); 
});   

的问题是它的第一个跳跃到锚,然后改变类,所以它的跳跃到div的位置时,它的“关闭”,而不是当它是“开” - 这有道理吗?有没有人有什么建议?


增加了一些额外的信息:

HTML:

<section class="form-popup on" id="request-trial"> 
<div class="container"> 
<h1>Let's Talk more!</h1> 
<p>Have a project in mind or have an organizational challenge you need assistance in tackling? Get in touch... we’ll discuss ways that the Beyond Nines team could help.</p> 

<a class="close">Close</a> 
</div> 
</section> 

Link是这样的:

<a href="#request-trial">Sign up</a> 

的CSS:

.form-popup, 
.form-popup.on { 
position: absolute; 
height: 500px; 
z-index: 0; 
bottom: 0px; 
opacity: 1; 
} 

.form-popup.off { 

bottom: -580px; 
opacity: 0; 

    } 
+0

类可能是事先改变,但幻灯片动画需要一个非零量的时间来完成。请张贴您的HTML,以便尝试解决问题。 –

+0

你可能是对的 - 有什么办法可以延迟它吗? – user1851361

+0

@ user1851361我认为我的答案是你正在寻找的:http://jsfiddle.net/Ag98N/ –

回答

0

这里是我的代码

$('a[href="#request-trial"]').click(function(evt) { 
    evt.preventDefault(); 
    var target = $("#request-trial"); 
    target.toggleClass("on off"); 
    if (target.length > 0 && target.hasClass("on")) 
    { 
    $("html, body").animate({ 
     scrollTop: target.offset().top - $(window).height() 
    }, 500); 
    } 
}); 
+0

这设置了类,但doesn没有跳转到div后... – user1851361

+0

我已经更新了我的答案。 –

+0

谢谢 - 同样的结果很不幸.. – user1851361

0

试试这个:

http://jsfiddle.net/Ag98N/2/

var documentBody = (($.browser.chrome)||($.browser.safari)) ? document.body : document.documentElement; 
     $('a[href="#request-trial"]').click(function (e) { 
     e.preventDefault(); 
     var $targetElem = $("#request-trial") 
     $targetElem.toggleClass("on off"); 
      if($targetElem.is('.on')) 
     $(documentBody).animate({scrollTop: $targetElem.offset().top}, 50); 
     }); 
+0

这跳转到原始.off位置 - 与我的原始代码 – user1851361

+0

相同如果我理解你,请尝试更新答案然后 –

+0

谢谢,但仍然无法工作 - 它必须与定位有关 - 我尝试设置#request-在你的jsfiddle试用,它停止工作,在我的网站上的结果相同。 – user1851361

相关问题