简体   繁体   English

更改隐藏div的滚动

[英]Change the scroll of an hidden div

I have an hidden div with a fixed height and a scrollbar. 我有一个固定高度的隐藏div和一个滚动条。 I would like to change the scroll position, but the browser won't le me do it, as the div is hidden. 我想改变滚动位置,但浏览器不会让我这样做,因为div是隐藏的。 The scrollTop property will stick to 0. scrollTop属性将保持为0。

Also, I don't want to show and hide the div back, which causes flickering. 此外,我不想显示和隐藏div返回,这会导致闪烁。

If anyone knows how to do it, it'll be really helpful. 如果有人知道该怎么做,那将非常有帮助。

Thanks! 谢谢!

You can save the scroll using jQuery's data function. 您可以使用jQuery的数据功能保存滚动。

function SaveScroll(val)
{
    $(the_element).data("Scroll", val);
}

function Show()
{
    var element = $(the_element);

    // prevent from showing while scrolling
    element.css
    ({
        position: "absolute",
        top: "-50000px",
        left: "-50000px",
        display: ""
    });

    // Scroll to the position set when it was hidden
    element.scrollTop(element.data("Scroll"));

    // show the element
    element.css
    ({
        position: "",
        top: "",
        left: ""
    });
}

This might do the trick 这可能会成功


You maybe can just use visibility: hidden instead of display: none . 您可以使用visibility: hidden而不是display: none The visibility keeps the element where it is. 可见性使元素保持原样。 I believe it is the exact same thing as opacity: 0 , but it is a cross-browser solution. 我相信它与opacity: 0完全相同opacity: 0 ,但它是一个跨浏览器的解决方案。

To prevent div flickering and fix <div> unavailability for scripts you can use position hiding instead of "display: none;" 为防止div闪烁并修复脚本的<div>不可用,您可以使用位置隐藏而不是"display: none;" :

.hidden {
  position: absolute !important;
  left: -9999px !important;
  top: -9999px !important;
}

My question was not complete. 我的问题不完整。 The div is not hidden on his own. div不是自己隐藏的。 It's part of a container div, which is hidden. 它是隐藏的容器div的一部分。 The inner div displays with his parent. 内部div与他的父母一起显示。

<div class="container hidden">
    <div id="some_div">Content</div>
    <div id="my_div">I wanted to scroll this one</div>
    <div id="other_div">Content</div>
</div>

We use jQuery came up with a custom "onShow" event. 我们使用jQuery想出了一个自定义的“onShow”事件。

So now we can do this: 所以现在我们可以这样做:

$('#my_div').bind('show', function() {
    handle_scrollTopOffset();
});

When the show event is bound, it adds the class .onShow to the div. 当绑定show事件时,它会将类.onShow添加到div。 And the jQuery.fn.show() function has been overridden to trigger the 'show' event on the childs which have the .onShow class. 并且重写了jQuery.fn.show()函数以触发具有.onShow类的子.onShow上的“show”事件。

Thanks everyone for their suggestions. 谢谢大家的建议。 I'm sorry I provided a uncomplete question. 对不起,我提供了一个不完整的问题。 I'll give all the details next time. 我下次会给出所有细节。

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

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