图像加载到页面后调整滚动条时出现问题
问题描述:我得到了一个带有表格的页面,其中包含文本和缩略图。通常,表格包含的条目数量多于屏幕所能容纳的条目数量,从而导致右侧出现滚动条。到目前为止没有问题。当加载页面或选择表格的下一页(分页)时,表格将被渲染 - 滚动条位于页面底部,我希望它在完全加载后位于。然后就会显示缩略图。由于它们的尺寸比表格中的文本稍大,因此表格的高度会变大,导致滚动条被设置在页面中间的某个位置。
加载图像后的页面和表格,您可以看到滚动条位于页面中间(垂直)的某个位置:
我不想摆弄缩略图大小。客户习惯了实际的设计和图像/图标尺寸。
使用分页功能,表格是页面上唯一被替换的元素。不幸的是,表上的“onload”不起作用。
如何才能在加载图像后显示滚动条(导致滚动条的正确位置)? 有没有办法在表格完全加载后将滚动条设置到页面底部?
Problem description: I got a page with a table containing text and thumbnails. Usually the table contains more entries than would fit on the screen leading to a scrollbar on the right side. No Problem so far. When loading the page or choosing the next page of the table (pagination) the table gets rendered - the scrollbar is at the bottom of the page where i would like it to be after complete load. Then the thumbnails are getting shown. Due to the fact that they are a bit bigger in size than the text in the table the table gets bigger in heigth leading to the scrollbar being set somewhere in the middle of the page.
Page and table after the images have been loaded, as you can see the scrollbar is somewhere in the middle (vertical) of the page:
I do not want to fiddle around with the thumbnail size. Customers are used to the actual design and image/icon sizes.
Usign the pagination function the table is the only element that gets replaced ont he page. "onload" on tables does not work unfortunatly.
What can i do to have the scrollbar appear after the images have been loaded (leading to the correct placement of the scrollbar)?
Is there a way to set the scrollbar to the bottom of the page after the table has been fully loaded?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有 jQuery 插件可以等待所有图像加载,但我无法让它们快速工作,也许你可以: 此处和此处。
但是,您也可以使用以下技巧:观察表格高度,如果它发生变化,则滚动到底部:
请参阅我的 demo fiddle 作为一个工作示例。
There are jQuery plugins to wait for all images to be loaded, but I couldn't get them working on the quick, maybe you can: here and here.
However, you also could use the following hack: watch for the table height and if it changes, scroll to the bottom:
see my demo fiddle for a working example.
最佳实践是在 HTML 或 CSS 中设置缩略图的宽度,如果所有缩略图的大小相同,您可以添加类似的样式
,或者,如果它们的大小可能不同,则必须添加
width
和height
属性。另一个解决方案是:
在 DOMload 上查看文档的滚动顶部,看看它是否位于底部,然后在
onload
事件(加载所有图像时会触发)再次检查是否滚动到如果需要的话,可以选择所需的位置。但我建议始终设置图像的尺寸,这样页面加载时就不会跳转。
编辑:如果您要动态加载图像,则可以执行两件事:
onload
,但是,您仍然需要使用document.createElement('img')
,这样您就可以确保所有图像都已加载。无论如何,在这些情况下,您应该对每个图像使用类似的内容:
注意,您必须在附加事件后设置
.src
,否则 Opera 中可能会出现一些问题。The best practice is to set the widths of thumbnails in HTML or CSS, if all the thumbnails are of the same size, you can just add the style like
Or, if they size can vary, you must add
width
andheight
attributes to the ` tag.Another solution is to:
Look at the document's scrolltop on DOMload, and look if it's at the bottom, then on
onload
event (which would be fired when all the images loaded) check again and if scroll to the desired position if needed.But I recommend always set the dimensions for images, so the page wouldn't jump when they are loaded.
Edit: If you're loading images dynamically, you can do two things:
onload
for images, however, you still would need to use thedocument.createElement('img')
, so you could be sure that all the images are loaded.Anyway, in these cases you should use something like that for each image:
Note, that you must set
.src
after attaching the event, or there could be some problems in Opera.