【问题标题】:toggle on div jumping back to top of page切换 div 跳回页面顶部
【发布时间】:2015-01-27 23:43:58
【问题描述】:

我有一个带有可点击 div 的页面,点击时会切换内容。第二次单击 div 时(关闭内容),页面跳回顶部。

我看到这是其他人遇到的问题,但是我发现的所有解决方案都与锚标签有关,这不是这里的问题。

我尝试根据解决方案here 更改我的代码并添加return false;event.preventDefault();,但这些都不起作用。我做错了什么?

jQuery:

    $(document).ready(function(){
        $('h1').animate({"right":"147px"},3000);
        $('.more').toggle(
                function(){
                    $(this).css({
                        'z-index':'100',
                        'background-position': '41px 0px'
                        });
                    $('#heroFullColor').hide();
                    $('#heroNoColor').show();
                    $('div.' + $(this).attr('id')).fadeIn('fast');
                    $('.more').not(this).hide();
                },
                function(){
                    $(this).css({
                        'background-position': '0px 0px'
                            });
                    $('div.' + $(this).attr('id')).fadeOut('fast');                 
                    $('#heroNoColor, .infoWindow').hide();
                    $('#heroFullColor').fadeIn('fast');                 
                    $('.more').not(this).show();
                });
    });

HTML 结构(在页面上重复多次):

    <div class="more" id="franceFlag"></div>
    <div class="infoWindow franceFlag">
        <img class="imageOn" src="images/lp_foundry_on-franceFlag.jpg" width="71" height="213" alt="French Flag" id="franceFlagOn" />
        <p class="franceFlag">blah blah blah</p>
    </div>      
    <div class="more" id="heliBoth"></div>
    <div class="infoWindow heliBoth">
        <img class="imageOn" src="images/lp_foundry_on-heliBoth.jpg" width="296" height="750" alt="Helicopters" id="heliBothOn" />
        <p class="heliBoth">blah blah blah</p>
    </div>      

Here is a fiddle - 如果你滚动到右下角,然后点击旗帜旁边的 + 号,你就会明白我的意思了。

【问题讨论】:

标签: jquery html


【解决方案1】:

这是因为您隐藏了赋予容器高度的元素,然后显示了不同的元素。所以它暂时没有高度。

如果您明确地为容器指定高度,则不会发生这种情况。例如。

#lp-foundry-container {
height: 940px;
}

第一次点击不会发生同样的事情,因为您会立即进行隐藏和显示。如果你要改变

$('#heroNoColor').show();

$('#heroNoColor').fadeIn('fast');

就像在关闭功能上一样,那么在第一次点击时也会发生同样的事情。

【讨论】:

  • 很高兴能正确解释它。不过,使用纯 CSS 修复确实需要一些思考,因为在携带元素的“hide()”之后事情往往会跳来跳去。如果布局允许,这仍然是要走的路。
  • 确实,在实践中通常不是那么简单——在执行 hide() 之前,还可以选择使用 javascript 设置高度。但如果可能的话,纯 CSS 是不错的。
  • 谢谢!我选择了这个答案,因为我同意 - 如果可能的话,一个纯 CSS 的解决方案很好。
【解决方案2】:

试试这个


JQuery

$(document).ready(function(){
        $('h1').animate({"right":"147px"},3000);
        $('.more').toggle(
                function(){
                    $(this).css({
                        'z-index':'100',
                        'background-position': '41px 0px'
                        });
                    $('#heroFullColor').hide();
                    $('#heroNoColor').show();
                    $('div.' + $(this).attr('id')).fadeIn('fast');
                    $('.more').not(this).hide();
                },
                function(){
                    $(this).css({
                        'background-position': '0px 0px'
                            });
                    $('div.' + $(this).attr('id')).fadeOut('fast');                 
                    $('#heroNoColor, .infoWindow').hide();
                    $('#heroFullColor').fadeIn('fast');                 
                    $('.more').not(this).show();
                });

                //What I added
                $(".more").click(function()
                {
                    var div = $(this);
                    $(window).scrollTop(div.offset().top).scrollLeft(div.offset().left);
                });
    });

基本上,您只是获取单击的 div 的当前顶部和左侧位置,并在完成后将页面设置为在该点定位。

Fiddle Demo

顺便说一句,这是一个很酷的主意,干得好!

【讨论】:

  • 感谢您的参与!这对我来说很有意义,但与其他解决方案一起使用,因为 CSS-only 在 possibl.e 时很好
【解决方案3】:

我知道的唯一解决方案是:

  $(document).ready(function(){
        ....
        $('.more').toggle(
                function(){
                   ...
                },
                function(){
                  var tempTop = $(window).scrollTop(); //<----remember Y
                  var tempLeft = $(window).scrollleft(); //<----remember X
                  .... your code here ...
                  //----> now restore the position
                  $(window).scrollTop(tempTop);
                  $(window).scrollLeft(tempLeft);

                }
        );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多