简体   繁体   English

Javascript / XHTML-使用div:hidden发现div中内容的总宽度

[英]Javascript/xhtml - discovering the total width of content in div with overflow:hidden

I'm writing a javascript based photo gallery with a horizontally scrollable thumbnail bar. 我正在编写一个具有水平滚动缩略图栏的基于javascript的照片库。

>> My current work-in-progress is here << >>我当前的工作正在进行中<<

I would like the thumbnail bar to stop scrolling when it gets to the last thumbnail. 我希望缩略图栏在到达最后一个缩略图时停止滚动。 To do this I need to find the total width of the contents of the div - preferably without adding up the widths of all the thumbnail images and margins. 为此,我需要找到div内容的总宽度-最好不累加所有缩略图和边距的宽度。

I've put an alert in my window.onload function so I can test the various element dimension functions in different browsers. 我在window.onload函数中添加了警报,以便可以在不同的浏览器中测试各种元素尺寸函数。 currently it's showing the value of scrollWidth, which is reported as 1540px by IE and 920px by FireFox, Safari, Opera, etc. 当前,它显示的是scrollWidth的值,IE将该值报告为1540px,FireFox,Safari,Opera等报告为920px。

The value 1540 is the correct one for my purposes, Can anyone tell me how to obtain this value in FireFox, etc. 值1540对于我而言是正确的值,谁能告诉我如何在FireFox中获取此值,等等。

Maybe you're just referencing the wrong element. 也许您只是在引用错误的元素。

document.getElementById('thumb_window').scrollWidth

is giving me 1540 on that page in both IE6 and firefox 2. Is that what you're looking for? 在IE6和firefox 2中都在该页面上给我1540。这是您要找的内容吗?

BTW in IE6 the thumbnails extend way past the right scroller. 顺便说一句,在IE6中,缩略图已超出右侧滚动条。

I think you're looking for "offsetWidth". 我认为您正在寻找“ offsetWidth”。 For a cross-browser experience its either scroll[Width/Height] or offset[Width/Height], whichever is greater. 对于跨浏览器,请体验其scroll [Width / Height]或offset [Width / Height]中较大的一个。

var elemWidth = (elem.offsetWidth > elem.scrollWidth) ? elem.offsetWidth : elem.scrollWidth;
var elemHeight = (elem.offsetHeight > elem.scrollHeight) ? elem.offsetHeight : elem.scrollHeight;

You can loop through the images on server-side and add up their width. 您可以在服务器端遍历图像并增加其宽度。 (In php you can use the getImagesize() function to do this). (在php中,您可以使用getImagesize()函数执行此操作)。 Then you can have the total width of all the image passed on to javascript. 然后,您可以将所有图像的总宽度传递给javascript。 Eg: 例如:

$total_width=0;
foreach ($images as $image)
{
   $info=getimagesize($image['file_path'];
   $total_width=$total_width +  $info[0];
}

In the javascript: 在javascript中:

<script type="text/javascript">
var total_width=<?=$total_width;?>;
</script>

What you should do is find the last image element in the div, get its X position relative to its container, add its width, and perhaps add its horizontal margins if it has any. 您应该做的是找到div中的最后一个图像元素,获得其相对于其容器的X位置,添加其宽度,或者添加水平边缘(如果有)。

Pseudo-code: 伪代码:

total_width = (last_image.x - container.x) + last_image.width + last_image.left_margin + last_image.right_margin

This would be fairly easy to accomplish using JQuery since it specifically has built in functionality for getting .last(), getting relative positioning, and fetching margin widths. 使用JQuery可以很容易地做到这一点,因为它特别内置了获取.last(),获取相对位置和获取边距宽度的功能。

Here is a little table:- 这是一张小桌子:-

thumb_window             thumb_slide
             scrollWidth offsetWidth  scrollWidth offsetWidth
IE7          1540        920          1540        1540
FF 3.05      1540        920          920         920
Opera 9.5    1540        920          920         920
Safari 3.2   1540        920          1540        920
Chrome 1.0   1540        920          1540        920

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

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