图像加载到页面后调整滚动条时出现问题

发布于 2024-11-29 19:33:18 字数 590 浏览 0 评论 0原文

问题描述:我得到了一个带有表格的页面,其中包含文本缩略图。通常,表格包含的条目数量多于屏幕所能容纳的条目数量,从而导致右侧出现滚动条。到目前为止没有问题。当加载页面或选择表格的下一页(分页)时,表格将被渲染 - 滚动条位于页面底部,我希望它在完全加载后位于。然后就会显示缩略图。由于它们的尺寸比表格中的文本稍大,因此表格的高度会变大,导致滚动条被设置在页面中间的某个位置。

加载图像后的页面和表格,您可以看到滚动条位于页面中间(垂直)的某个位置: 在此处输入图像描述

我不想摆弄缩略图大小。客户习惯了实际的设计和图像/图标尺寸。

使用分页功能,表格是页面上唯一被替换的元素。不幸的是,表上的“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:
enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

椵侞 2024-12-06 19:33:18

有 jQuery 插件可以等待所有图像加载,但我无法让它们快速工作,也许你可以: 此处此处

但是,您也可以使用以下技巧:观察表格高度,如果它发生变化,则滚动到底部:

var lastHeight;
function watchTable(){
    var currentHeight = $('#myTable').height();
    if(currentHeight !== lastHeight){
        lastHeight = currentHeight;
        $('#myDiv').scrollTop(currentHeight);
    }
}

setInterval(watchTable, 100);

请参阅我的 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:

var lastHeight;
function watchTable(){
    var currentHeight = $('#myTable').height();
    if(currentHeight !== lastHeight){
        lastHeight = currentHeight;
        $('#myDiv').scrollTop(currentHeight);
    }
}

setInterval(watchTable, 100);

see my demo fiddle for a working example.

梦中的蝴蝶 2024-12-06 19:33:18

最佳实践是在 HTML 或 CSS 中设置缩略图的宽度,如果所有缩略图的大小相同,您可以添加类似的样式

.thumbnail {
    width: 150px;
    height: 100px;
}

,或者,如果它们的大小可能不同,则必须添加 widthheight 属性。

另一个解决方案是:

在 DOMload 上查看文档的滚动顶部,看看它是否位于底部,然后在 onload 事件(加载所有图像时会触发)再次检查是否滚动到如果需要的话,可以选择所需的位置。

但我建议始终设置图像的尺寸,这样页面加载时就不会跳转。

编辑:如果您要动态加载图像,则可以执行两件事:

  1. 预加载图像,然后以正确的尺寸插入它们。
  2. 对图像使用 onload,但是,您仍然需要使用 document.createElement('img'),这样您就可以确保所有图像都已加载。

无论如何,在这些情况下,您应该对每个图像使用类似的内容:

var image = document.createElement('img');
image.onload = function () {
    // Image is loaded
};
image.src = 'test.jpg';

注意,您必须在附加事件后设置 .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

.thumbnail {
    width: 150px;
    height: 100px;
}

Or, if they size can vary, you must add width and height 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:

  1. Preload images and then insert them with the right dimensions.
  2. Use onload for images, however, you still would need to use the document.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:

var image = document.createElement('img');
image.onload = function () {
    // Image is loaded
};
image.src = 'test.jpg';

Note, that you must set .src after attaching the event, or there could be some problems in Opera.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文