简体   繁体   English

jQuery Tabs-防止锚点在单击时将页面滚动到顶部

[英]jQuery Tabs - Preventing an anchor from scrolling the page to the top on click

I have set up a fiddle which demonstrates my issue ( http://jsfiddle.net/PR2Yn/ ). 我设置了一个小提琴来演示我的问题( http://jsfiddle.net/PR2Yn/ )。 I tried the prevent function, but to no avail. 我尝试了阻止功能,但无济于事。

<div id="horizontalTab">
<ul class="resp-tabs-list">
    <li> <a href="#" class="accordionNav residents" tabindex="0">Residents<div class="tri"></div></a> </li> 
    <li> <a href="#" class="accordionNav business" tabindex="0">Business<div class="tri"></div></a> </li> 
    <li> <a href="#" class="accordionNav visitors" tabindex="0">Visitors<div class="tri"></div></a> </li> 
    <li> <a href="#" class="accordionNav wealden247" tabindex="0">Wealden 24/7<div class="tri"></div></a> </li> 
    <li> <a href="#" class="accordionNav payforit" tabindex="0">Pay for it<div class="tri"></div></a> </li>
    <li> <a href="#" class="accordionNav applyforit" tabindex="0">Apply for it<div class="tri"></div></a> </li>
    <li> <a href="#" class="accordionNav reportit" tabindex="0">Report it<div class="tri"></div></a> </li>
    <li> <a href="#" class="accordionNav planning" tabindex="0">Planning<div class="tri"></div></a> </li>
    <li> <a href="#" class="accordionNav bins" tabindex="0">Bins<div class="tri"></div></a> </li> 
    <li> <a href="#" class="accordionNav council" tabindex="0">Your Council<div class="tri"></div></a> </li> 
</ul>

Specifically, the news tabs at the bottom of the page jumps to the top of the page when they are clicked. 具体来说,单击页面底部的新闻选项卡会跳至页面顶部。 This is fine as long as you have not scrolled down, but if you have scrolled down causes an issue as you have to continually find where you were. 只要您没有向下滚动就可以了,但是如果向下滚动会导致问题,因为您必须不断查找自己的位置。

You need to define 您需要定义

href='javascript:void(0);'

instead of 代替

 href='#'

in all the anchor tags surrounding news image and content. 在围绕新闻图片和内容的所有锚标签中。

If you define href='#' in anchor, it will add # at the end of the url of your page and jumps up. 如果您在锚点中定义href ='#',它将在页面网址的末尾添加#并跳转。

Looking at your fiddle, it seems the problem you are having is not the usual jump/scroll (by the browser in response to an onclick event handler), but rather a problem with the tabs: every tab that is opened, the previously clicked one (even if clicked to close it) also opens. 查看您的小提琴,看来您遇到的问题不是通常的跳转/滚动(浏览器响应onclick事件处理程序),而是选项卡的问题:打开的每个选项卡,先前单击的选项卡(即使单击以将其关闭)也会打开。 You can see this by doing the following: 您可以通过执行以下操作来查看:

1) Click on "Residents" tab to open it;
2) Click on it again to close it;
3) Now scroll down and click on another tab (such as "Apply for it");
4) Then you will be scrolled up because, in fact, "Residents" is also reopened.

I thought this was being caused by some issue in your javascript code or HTML, but I could not find any problem. 我认为这是由您的JavaScript代码或HTML中的某些问题引起的,但我找不到任何问题。 In a test, I deleted most of your javascript and HTML, maintaining only a very basic easytabs call, and the problem persisted. 在一个测试中,我删除了您的大多数javascript和HTML,仅维护了一个非常基本的easytabs调用,问题仍然存在。

So I thought the problem might be with the easyResponsiveTabs.js plugin itself. 所以我认为问题可能出在easyResponsiveTabs.js插件本身。 So I changed the external resource in a fiddle, using the one used by the "official" easyResponsiveTabs example page and the problem is gone. 因此,我使用“官方的” easyResponsiveTabs 示例页面使用的外部资源来更改了外部资源,问题不再存在。 You can see your tabs working here: http://jsfiddle.net/8gWe9/ 您可以在此处查看标签页: http : //jsfiddle.net/8gWe9/

In sum, it looks like the problem is with the easyResponsiveTabs.js you are using, though I have not checked that in detail. 总而言之,似乎问题出在您使用的easyResponsiveTabs.js,尽管我没有详细检查。 It could be caused by using an old version of it, but it could also be caused by a problem with your web server not delivering the .js file correctly. 这可能是由于使用了旧版本引起的,也可能是由于Web服务器无法正确交付.js文件而引起的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM