检查缩略图和图像是否存在
我正在尝试在需要加载三个图像之一的页面中编写一些 JavaScript。首先,它需要检查图片的缩略图是否存在并显示,如果缩略图不存在,则检查 full_size 图像并显示该图像(缩小),如果缩略图或全尺寸图像都不存在,使用 noimage.gif 作为小占位符。做到这一点最有效的方法是什么?
有些规定,不能在服务器端执行,因为没有服务器。该 java 脚本将作为 .kml 文件插入到 kml 文件中。我正在绘制光纤网络,这是用于手孔部分的。基本上,每个地标将有 1 到 5 张图片,图片的名称将与地标名称相同,每张图片后面带有 -1、-2、-3 等。因此,每个地标都从相同的气球样式中获取其样式......并且我在此气球样式中编写的 html 调用 img src="./images/$[name]-1.jpg" width="120px" height ="100px" 其中 $[name] 插入地标的名称。这将允许用户在更新 kml 文件并添加新地标时,他们所要做的就是重命名 jpeg 以匹配地标的名称,并在其末尾添加 -1 并将其转储到图像文件夹中....这样用户就不必编辑任何html(这对他们来说太多了)
I am attempting to write some javascript in a page that needs to load one of three images. First it needs to check to see if a Thumbnail for a picture exists and display that, if the thumbnail does not exist, check for the full_size image and display that (scaled down) and if neither a thumbnail or a full-size image exists, use a noimage.gif as a small placeholder. What would be the most efficient way to do this?
Some stipulations, can't do it server side as there is no server. This java script is going to be inserted into a kml file as a . I am mapping a fiber network and this is for the hand-hole portion. Basically, each placemark will have any where from 1 to 5 pics and the names of the pics will be the same as the placemark name with a -1, -2, -3, etc. after each picture. So each of these placemarks gets its style from the same balloonstyle.... and the html I have written in this balloon style calls img src="./images/$[name]-1.jpg" width="120px" height="100px" where $[name] inserts the name of the placemark. This will allow for the users, when they update the kml file and add new placemarks, all they will have to do is rename the jpeg to match the placemark's name and add a -1 to the end of it and dump it in the images folder.... making it so the user doesn't have to edit ANY html (which is too much for them to do)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会在服务器上进行检查,然后让服务器提供可用图像的资源路径。
I would do the checking on the server, then have the server provide a resource path to the available image.
我同意@Jason Dean的观点,
但是如果你想使用 JavaScript 来检测它,我会这样做:
获取缩略图时,我会附加某种 id/class 属性并检查它是否存在。
检查“img-tumb”的 id 是否存在:
现在检查图像大小:(放置在最后一个的 else 之后if)
要在检测到图像高度后更改高度:
对于您的结局事件(当未检测到任何事件时),我将使用与第一阶段相同的检查。
完整代码:
I agree with @Jason Dean,
But If you want to detect it by using JavaScript I would do like so:
When getting the thumbnail I would attach some kind of id/class property to it and check if it's present.
Check if id of "img-tumb" exists:
Now to check the image size:(Placed after else of last if)
To change the height after the image height was detected:
For you finale event(when none are detected), I would use the same checking as in phase one.
Complete code: