单击时滑动面板跳回主页

发布于 2024-11-18 19:45:14 字数 265 浏览 7 评论 0原文

我在一个 1page 网站上制作了一个垂直滑动面板(点击右上角的 psssst)。 总而言之,它工作正常,上下滑动正常(滑动时按钮上仍然有一点凹凸,但没什么大问题)。

问题是,当您向右滚动然后单击滑动面板时,它会跳回到页面的开头。

我该如何解决/防止这个问题? 您可以在这里查看问题:http://www.basenharald.nl/3d

提前致谢!

I made a vertical sliding panel on a 1page website (click psssst on the top right).
All in all it works fine, it slides up and down properly (still a little bump on the button when sliding, but no biggy).

The problem is when you scroll to the right and then click the sliding panel it jumps back to the beginning of the page.

How can i fix/prevent this?
You can view the problem here: http://www.basenharald.nl/3d

thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

真心难拥有 2024-11-25 19:45:14

看来您有两个问题:

第一个问题(我认为您已经修复了它)

锚点上的单击事件将您发送到“/#”URL。发生这种情况是因为锚元素的 href 为“#”,并且浏览器的默认行为是将您发送到该 URL。

为了防止发生这种情况,您应该在点击事件处理程序中使用e.preventDefault(),或者返回 false 如果您想要停止所有事件传播(因为这就是 jQuery 的工作原理)。

第二个问题

似乎即使在阻止默认行为之后,单击“#open”或“#close”仍然会使 Scroller 返回到初始状态。在 firebug 中,我可以看到这些元素上有一个处理程序,导致发生这种情况。您可以通过在 firebug 中运行它来亲自看到它:

console.log($('#close')[0].onclick.toString())

您会看到以下内容:

function () {
  Scroller.end(this);
  l = this.hash.substr(1);
  a = document.getElementsByTagName("a");
  for (i = 0; i < a.length; i++) {
    if (a[i].name == l) {
      clearInterval(Scroller.interval);
      Scroller.interval = setInterval("Scroller.scroll(" + Scroller.offsetParent(a[i]) + ")", 10);
    }
  }
}

似乎在代码中的某个位置您正在将事件处理程序分配给所有锚点,而该行

Scroller.interval = setInterval("Scroller.scroll(" + Scroller.offsetParent(a[i]) + ")", 10); 

就是导致 Scroller 的原因“重置”、“返回”或其他什么。

也许问题是有条件的?也许 if (a[i].name == l) 总是评估为 true 并最终将事件处理程序分配给所有 锚点尽管这不是它的意图。

我对您的应用程序和/或您正在使用的任何插件了解不够,所以我只能告诉您问题出在哪里,并且问题就在这个函数中。

希望这有帮助。

It seems that you have two problems:

The first problem (you've already fixed it, I think)

The click event on the anchor is sending you to the '/#' URL. This happens because the anchor element has an href of "#" and the default behavior of the browser is to send you to that URL.

In order to prevent that from happening, you should use e.preventDefault() in your click event handlers, or return false if you want to stop all event propagation (because that's how jQuery works).

The second problem

It seems that even after preventing the default behavior, clicking on '#open' or '#close' is still bringing the Scroller back to the initial state. In firebug I can see that there is a handler for the click event on those elements that is causing this to happen. You can see it yourself by running this in firebug:

console.log($('#close')[0].onclick.toString())

You'll see this:

function () {
  Scroller.end(this);
  l = this.hash.substr(1);
  a = document.getElementsByTagName("a");
  for (i = 0; i < a.length; i++) {
    if (a[i].name == l) {
      clearInterval(Scroller.interval);
      Scroller.interval = setInterval("Scroller.scroll(" + Scroller.offsetParent(a[i]) + ")", 10);
    }
  }
}

It seems that somewhere in your code you're assigning an event handler to all anchors, and the line

Scroller.interval = setInterval("Scroller.scroll(" + Scroller.offsetParent(a[i]) + ")", 10); 

is what's causing your Scroller to "reset", "go back" or something.

Maybe the problem is the conditional? Maybe if (a[i].name == l) is always evaluating to true and ends up assigning the event handler to all anchors even though that's not its intent.

I don't know enough about your app and/or whatever plugins you're using, so I can only tell you where the problem is, and it's in this function.

Hope this helps.

深居我梦 2024-11-25 19:45:14

尝试将 return false; 添加到您的点击处理程序中:

// Expand Panel
$("#open").click(function(){
    $("div#panel").slideDown("slow");
    return false;
}); 

// Collapse Panel
$("#close").click(function(){
    $("div#panel").slideUp("slow");
    return false;
}); 

Try adding return false; to your click handlers:

// Expand Panel
$("#open").click(function(){
    $("div#panel").slideDown("slow");
    return false;
}); 

// Collapse Panel
$("#close").click(function(){
    $("div#panel").slideUp("slow");
    return false;
}); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文