使用 JQuery PreventDefault(),但仍将路径添加到 URL
我正在开发一个网站,点击某个链接将会滑下登录面板。我使用 event.preventDefault()
来阻止网站重定向,并使用动画事件来向下滑动面板。单击链接时,面板向下滑动,url 保持不变。
单击链接时我希望面板正常显示动画,但链接的 href 属性显示在 url 中。在这种情况下,它会是这样的:http://domain_name/#login
。
这是我现在要执行的代码:
$("#login_link").click(function (e) {
e.preventDefault();
$("#login").animate({ 'margin-top': 0 }, 600, 'linear');
window.location.hash = $(this).attr('href');
});
此代码成功地将“#login”添加到所需的 URL,但它跳过了登录面板的动画。单击链接时,面板会立即出现。我想保留动画和更新的网址行为。这可能吗?
I am working on a website where clicking on a certain link will slide down a login panel. I am using event.preventDefault()
to stop the site from redirecting as well as an animation event to slide the panel down. When the link is clicked, the panel slides down and the url remains unchanged.
What I want to happen when the link is clicked is for the panel to animate as normal, but for the href attribute of the link to be shown in the url. In this case it would be something like this: http://domain_name/#login
.
Here is the code I've got going right now:
$("#login_link").click(function (e) {
e.preventDefault();
$("#login").animate({ 'margin-top': 0 }, 600, 'linear');
window.location.hash = $(this).attr('href');
});
This code successfully adds the '#login' to the url as desired, but it skips the animation of the login panel. When clicking the link the panel just appears instantly. I would like to keep both the animation and the updated url behaviors. Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用下面的代码。只需调用动画完成事件中的哈希即可。
use below code . just call the hash in animation completed event.
我不确定,但您可能正在寻找此功能
1>当某些主体点击登录时,url 更改为 abc.com/#login,并且出现登录面板动画
或
2 >当某些机构只是在网址中输入 abc.com/#login 时,您会期望相同的行为(登录面板的动画)。
您当前所做的操作仅适用于 case1,不适用于 case2。如果您希望动画(或其他)在单击或仅输入网址时同时起作用。您需要某种机制来检测哈希更改。
有一个著名的插件 http://benalman.com/projects/jquery-bbq-plugin/< /a>
去看看这个插件,看看事情是如何完成的。如果您发现任何难以理解的内容,我们就在这里!
I am not sure but you may bee looking for this feature
1> when some body clicks login, url changes to abc.com/#login, and login panel animation occurs
or
2 > when some body just types abc.com/#login in url, you expect the same behavior (animation of login panel).
What you are doing currently will work only for case1 and wont work for case2. If you want animation (or whatever) to work both, when clicked , or just url typed. You need some mechanism to detect hash changes.
There is a famous plugin http://benalman.com/projects/jquery-bbq-plugin/
Go and see this plugin and, look how things are done. If you find anything tough to understand we are here!!