【问题标题】:Hide all images except first one till page is fully loaded隐藏除第一个之外的所有图像,直到页面完全加载
【发布时间】:2015-10-03 01:52:19
【问题描述】:

我的页面上有一个滑块,它是这样实现的:

<ul class="slides" id="slides">
    <li class="slide"><a href=""><img src="1.jpg" title="" alt="" /></a></li>
    <li class="slide"><a href=""><img src="2.jpg" title="" alt="" /></a></li>
    <li class="slide"><a href=""><img src="3.jpg" title="" alt="" /></a></li>
    <li class="slide"><a href=""><img src="4.jpg" title="" alt="" /></a></li>
</ul>

如果我加载页面,所有图像都会同时显示。只有在加载了整个页面之后,才会触发滑块脚本并隐藏除一个之外的所有图像。 我试图通过给滑块一个特定的高度来用 CSS 修复它。实际上这适用于桌面,但如果我调整浏览器窗口的大小则不行。

所以我想编写某种 JavaScript 来隐藏除第一个之外的所有图像,直到页面完全加载。

我用下面的脚本试过了,但它不起作用

<script>
jQuery(window).load(function() {
  jQuery(".slides li:not(:first-child)").hide(function() {
    jQuery(".slides li:not(:first-child)").show();
  });
});
</script>

谁能指出我正确的方向?

【问题讨论】:

  • 用 CSS 隐藏图像,并在 onload 处理程序中显示它们。
  • 为什么不直接使用 CSS?
  • 您可以考虑使用设置超时功能在设置的秒数后加载图像。

标签: javascript jquery


【解决方案1】:

使用 CSS

#slides li:not(:first-child) { display: none }

然后在加载时显示它们

$(window).load(function() {
    $(".slides li:not(:first-child)").show();
});

【讨论】:

  • CSS 中应该有#slides li:not(:first-child) { display: none },因为img:first-child 将应用于所有这些图像——它们是a 元素中的第一个子元素。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-21
  • 1970-01-01
相关资源
最近更新 更多