我想使用 jquery 来检测是否在元素的 style 属性中设置了图像背景,如果存在则确定图像的宽高比并将相关类添加到元素。
http://jsfiddle.net/2urn/9ydHG/37/
<article class="listing-article" style="background-image:
url('http://placehold.it/600x350/ff6600/fff');">
<div class="overlay"></div>
<div class="article-summary">
<h2 class="post-title post">Horizontal Background</h2>
<section class="article-excerpt">
<h3 class="subhead">Subhead</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam.</p>
</section>
</div>
</article>
我在fiddle的js窗口中引用了一些相关的堆栈文章,但是代码对我的水平来说太复杂了。
非常感谢任何帮助。
最佳答案
你可以试试这样的——
var backgroundImg = $('article.listing-article').css("background-image");
if (typeof(backgroundImg) != "undefined") {
// background image present, do stuff here
}
关于javascript - 检测背景图片属性,检测背景图片比例,添加相关类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24975108/