简体   繁体   English

为什么有时scrollTop / scrollLeft不可写?

[英]Why sometime scrollTop/scrollLeft not writable?

I'm using dhtmlx Gantt Chart UI component which have task list and graphical chart. 我正在使用具有任务列表和图形图表的dhtmlx甘特图UI组件。 Task list and graphical chart are contained in two separate div element which synchronized for parallel scrolling. 任务列表和图形图表包含在两个单独的div元素中,这些元素同步用于并行滚动。 By scrolling the chart area, task list is automatically scrolled resulting the task row position matches the Gantt line position. 通过滚动图表区域,任务列表会自动滚动,从而使任务行位置与甘特图行位置匹配。

Inspecting the component source code, I found the sync is implemented by the following code : 检查组件源代码,我发现同步是通过以下代码实现的:

this.oData.onscroll = function() {
    self.panelTime.scrollLeft = this.scrollLeft;
    self.panelNames.scrollTop = this.scrollTop;
};

All HTML markup in the UI control are generated dynamically by JavaScript. UI控件中的所有HTML标记都是由JavaScript动态生成的。 All is working well except it takes too long time to render 800-ish task list. 一切都运行良好,除了渲染800-ish任务列表需要很长时间。

To improve rendering time, I decide to built my own server side rendering module to generate the HTML markup identical with that originally generated client side. 为了缩短渲染时间,我决定构建自己的服务器端渲染模块,以生成与最初生成的客户端相同的HTML标记。 This markup is fetched from client side using ordinary jquery $.get() and injected to page using $(el).html(). 使用普通的jquery $ .get()从客户端获取此标记,并使用$(el).html()将其注入页面。 Then I put the necessary event handler as the original client side version. 然后我将必要的事件处理程序作为原始客户端版本。

The problem now is parallel scrolling doesn't work. 现在的问题是并行滚动不起作用。 I could capture the scroll event on the chart area, but I couldn't set the scrollTop property of the task list area. 我可以在图表区域捕获滚动事件,但我无法设置任务列表区域的scrollTop属性。 I test in firebug to manually force the scrollTop property, but the value didn't change. 我在firebug中测试手动强制scrollTop属性,但值没有改变。 It seems that scrollTop property is read-only. 似乎scrollTop属性是只读的。

Is there any explanation for this ? 这有什么解释吗?

You can't scroll down below the bottom (or past the right) of the element's contents. 您不能向下滚动元素内容的底部(或右侧)。 If the element has overflow: visible (the default), or is at least as large as its contents, then both scroll properties will be stuck at 0. Similarly, even if there is something hidden to scroll into view, then you won't be able to scroll past the end; 如果元素有overflow: visible (默认),或者至少与其内容一样大,那么两个滚动属性都将停留在0.同样,即使有隐藏的东西滚动到视图中,那么你也不会能够滚动到最后; if you set the scrollTop or scrollLeft to a larger value than makes sense, it will decrease to the largest value that makes sense. 如果将scrollTop或scrollLeft设置为更大的值而不是有意义,它将减小到有意义的最大值。

My guess is in your case you're trying to scroll the div before its content is loaded, and it's refusing to scroll because there isn't anything to scroll into view. 我的猜测是在你的情况下,你试图在加载内容之前滚动div,并且它拒绝滚动,因为没有任何内容可以滚动到视图中。

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

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