简体   繁体   English

单击功能使页面以奇怪的方式滚动(不在页面顶部)

[英]Click function making page scroll in odd way (not to top of page)

First time asker, long time lurker. 第一次申请者,长期潜伏者。

I'm doing a fadein/out toggle that displays 1 of 2 charts depending on which button you click. 我正在做一个淡入/淡出切换,根据您单击的按钮显示2个图表中的1个。

That bit works just fine, but I'm getting a weird page-jump glitch. 那一点工作得很好,但是我遇到了奇怪的页面跳转故障。 Now, it's not the usual jump-to-the-top behaviour. 现在,这不是通常的跳楼行为。 I have that part covered in the code, and it doesn't do that. 我在代码中覆盖了那部分,但它并没有做到这一点。

Every time I click on one of the toggles, the page scrolls downward to the point where the chart area is at the bottom of the window. 每次我单击其中一个切换按钮时,页面就会向下滚动到图表区域位于窗口底部的位置。

But it gets weirder. 但这变得奇怪了。 If I make the browser window very short or very narrow (it's a responsive site), it stops doing this glitch. 如果我使浏览器窗口非常短或非常狭窄(这是一个响应站点),它将停止出现此故障。 It's also not happening on iPhone or iPad at all, even though if I set the browser width to the same width as it would be on an iPad, the desktop browser still does the jumping. 即使我将浏览器的宽度设置为与iPad上的宽度相同,iPhone或iPad上也根本不会发生这种情况,但台式机浏览器仍然会跳跃。

There are no elements that are added/removed based on the viewport width in the area that's jumping around, and there are no anchor IDs that would be accidentally used as jump points. 在要跳转的区域中,没有根据视口宽度添加/删除的元素,也没有偶然被用作跳转点的锚点ID。

Unfortunately I can't show the actual page to you, but I can show the script and a bit of the HTML. 不幸的是,我无法向您显示实际页面,但是可以显示脚本和一些HTML。 The code for both toggles is the same, just with the IDs switched around. 两次切换的代码是相同的,只是切换了ID。

The script: 剧本:

    $('#left-toggle > a').click(function(c)
    {
        c.preventDefault();
        c.stopPropagation();
        $('#right-toggle').removeClass('toggle-active');
        $('#left-toggle').addClass('toggle-active');
        $(pricing_subscriptionID).fadeOut('fast', function(){
            $(pricing_singleID).fadeIn('fast', function(){

            });
        });
    });

The HTML for the toggles: 切换的HTML:

<div id="chart-toggle">
     <div id="left-toggle" class="toggle-active"><a href="#">Single Pricing</a></div>
    <div id="right-toggle"><a href="#">Subscription Pricing</a></div>
</div>

"toggle-active" is just for styling. “主动切换”仅用于样式设置。

Any ideas? 有任何想法吗?

It seems to be almost wanting to centre the toggles on the page, but it's not quite putting them in the middle either. 似乎几乎要在页面上居中放置切换开关,但也没有将它们置于中间。

JSFiddle: http://jsfiddle.net/TmrLw/ JSFiddle: http : //jsfiddle.net/TmrLw/

Probably its because the link's href is # which links to the top of the document. 可能是因为链接的href是#(其链接到文档顶部)。 try to remove the href attribute 尝试删除href属性

It's because of your link to # . 这是因为您链接到# Here are some ways you can fix this: 您可以通过以下几种方法解决此问题:

1. Replace "#" with something else 1.将“#”替换为其他内容

Instead of 代替

<a href="#">Subscription Pricing</a>

Try this: 尝试这个:

<a href="Javascript: void(0);">Subscription Pricing</a>

This will give you the cursor pointer you're looking for and avoid the page jump. 这将为您提供所需的光标指针,并避免页面跳转。

2. Create a class with the pointer effect 2.创建一个具有指针效果的类

If you use this CSS rule: 如果使用此CSS规则:

.pointer {
    cursor: pointer;
}

Then you can wrap your text with this class instead: 然后,您可以使用此类包装文本:

<div class="pointer">Subscription Pricing</div>

3. Remove the default effect of "#" 3.删除默认效果“#”

This Javascript will get rid of its default effect: 该Javascript将摆脱其默认效果:

$('a[href="#"]').click(function(e)
{
    // Cancel the default action
    e.preventDefault();
});

Hope this helps 希望这可以帮助

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

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