jquery - 是否可以使用 jQuery 延迟样式属性?

标签 jquery

这是我的代码:

<img class="animated fadeIn" src="https://example.com/img/image.png" style="background-image: url('.$this->url.'/thumb.php?src='.$row['background'].'&t=b&w=800&h=300&q=100)" />

我需要在页面加载后加载样式属性。

可以使用 jQuery 吗?

最佳答案

我会更改 html 以使用数据属性

<img class="animated fadeIn" src="https://example.com/img/image.png" data-style="background-image: url('.$this->url.'/thumb.php?src='.$row['background'].'&t=b&w=800&h=300&q=100)" />

然后在需要时将其应用到实际样式(使用 load 事件或超时)

$(window).on('load', function(){
   $('[data-style]').attr('style', function(){ return $(this).data('style'); });
});

这样代码将应用于您想要的任意数量的元素

<小时/>

超时版本会是这样的

$(window).on('load', function(){
   setTimeout(function(){
       $('[data-style]').attr('style', function(){ return $(this).data('style'); });
   }, 5000); // apply the style 5 seconds after the page is loaded.
});

关于jquery - 是否可以使用 jQuery 延迟样式属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35590808/

相关文章:

javascript - 粉碎一个元素,向随机方向发送碎片

javascript - Bootstrap 如何在下拉菜单关闭时刷新页面

javascript - 比较两个数组时不执行函数

javascript - jquery 绑定(bind)点击标签触发两次

javascript - 隐藏/显示具有特定类的表行,直到具有不同类名的另一行?

jquery - 使用 JQuery 将数组的数组合并为带有元素的单个数组

jQuery - 上下滑动特定距离

javascript - HTML5 自动滚动到下一部分

javascript - TypeError : abc. getAttribute 不是函数

javascript - 使用 JQuery 自动录制并允许播放一次