我可以使用 Jquery 查找然后设置图像的宽度吗?
我正在使用一个幻灯片脚本,它需要每个图像的尺寸才能正常工作。客户还需要添加大约 50 多张图像的尺寸。
我是否可以使用一个脚本来查找 ID 为“scroller”的无序列表中所有图像的宽度,然后设置宽度,就像在图像标签中设置的那样?
如果有人可以帮助我写它,并可能解释每个部分的作用,我将不胜感激。
如果您正在寻找代码,就在这里。
在 JQuery 代码之前。
<ul id="scroller">
<li><img src="image.jpg"></li>
<li><img src="image.jpg"></li>
<li><img src="image.jpg"></li>
<li><img src="image.jpg"></li>
</ul>
JQuery 代码之后。
<ul id="scroller">
<li><img src="image.jpg" width="100"></li>
<li><img src="image.jpg" width="350"></li>
<li><img src="image.jpg" width="70"></li>
<li><img src="image.jpg" wudth="250"></li>
</ul>
这些图像具有不同的宽度,但高度相同。
I am using a slideshow script that needs the dimensions of every image for it to work properly. There are going to be some 50+ images that a client would have to add dimensions too.
Is there a script I can use that will find the width of all the images in an unordered list with an ID of "scroller", and then set the width just like it'd be set within the image tag?
If somebody could help me write it, and possibly explain what each section does, I would greatly appreciate it.
And if you are looking for code, here it is.
Before JQuery code.
<ul id="scroller">
<li><img src="image.jpg"></li>
<li><img src="image.jpg"></li>
<li><img src="image.jpg"></li>
<li><img src="image.jpg"></li>
</ul>
After JQuery code.
<ul id="scroller">
<li><img src="image.jpg" width="100"></li>
<li><img src="image.jpg" width="350"></li>
<li><img src="image.jpg" width="70"></li>
<li><img src="image.jpg" wudth="250"></li>
</ul>
The images would have different widths, but all the same height.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,你可以。这确实非常直观。
为了循环遍历所有这些,您将使用
.each()
。您可能想知道您可以获取现有宽度并设置它,但只能在元素完全加载之后。这可能会在较旧的浏览器上导致一些跳跃,因此隐藏图像,获取现有尺寸,进行转换,然后将它们同时淡入或看起来像这样的漂亮东西是值得的。
谢谢@Whetstone
Yes you could. It's very intuitive really.
In order to loop through all of them, you'll use
.each()
.You may want to know that you can grab the existing width and set it, but only after the element is fully loaded. This may cause a bit of jumpiness on older browsers, so it would be worth it to hide the images, grab the existing dimensions, do the transformation, and then fade them all in at the same time or something pretty looking like that.
Thanks @Whetstone
.each 滚动浏览 #scroller
ul
中的所有图像。jQuery 只能在图像完全加载后才能获取图像的宽度,因此是
.load
。剩下的事情就非常简单了。使用 jQuery API 文档了解更多信息。
.each scrolls through all the images within the #scroller
ul
.jQuery can only get the width of the image after it has been fully loaded, hence the
.load
.The rest is pretty straightforward. Use the jQuery API docs for more information.