在JS中获取多个图像宽度

时间:2014-08-28 05:26:29

标签: javascript

我正在使用一个JS滑块,其中包含5张幻灯片,每张幻灯片都带有图片和标题。我想只在那个宽度为>的图像中添加一个类2xheight。以下是我的JS代码:

$(".slides img").each(function() {

    var w = $("img.gallery").width();
    var h = $("img.gallery").height() * 2;

    if (w > h) {
        $(this).addClass("auto");
    };                    
});

请帮我解决这个问题。提前致谢, 奇拉格。

以下是我的HTML代码:

<div class="flex-container">
                <div class="flexslider">
                    <ul class="slides">
                        <li>
                            <span data-picture data-alt="WK Credentials">
                                <span data-src="images/large/slide1.jpg" data-media="(min-width: 1601px)"></span>

                            </span>
                            <img src="images/slide5.jpg" id="myImg" alt="" title="" class="gallery" />
                            <p class="flex-caption">Captions and cupcakes. Winning combination.</p>
                        </li>
                        <li>
                            <center>
                                <div class="intro">
                                    <h1 class="head">Incredible India | Tiffin</h1>
                                    <p class="text">Use what you know about your users to determine whether the words 
                                    and phrases you plan to use are appropriate.</p>
                                </div>
                            </center>
                        </li>
                        <li>
                            <span data-picture data-alt="WK Credentials">
                                <span data-src="images/large/slide2.jpg" data-media="(min-width: 1601px)"></span>

                            </span>
                            <img src="images/slide2.jpg" id="myImg" alt="" title="" class="gallery" />
                            <p class="flex-caption">This image is wrapped in a link!</p>
                        </li>
                        <li>
                            <span data-picture data-alt="WK Credentials">
                                <span data-src="images/large/slide3.jpg" data-media="(min-width: 1601px)"></span>

                            </span>
                            <img src="images/slide3.jpg" id="myImg" alt="" title="" class="gallery" />
                            <p class="flex-caption">Captions and cupcakes. Winning combination.</p>
                        </li>
                        <li>
                            <span data-picture data-alt="WK Credentials">
                                <span data-src="images/large/slide4.jpg" data-media="(min-width: 1601px)"></span>

                            </span>
                            <img src="images/slide4.jpg" id="myImg" alt="" title="" class="gallery" />
                            <p class="flex-caption">Captions and cupcakes. Winning combination.</p>
                        </li>
                        <li>
                            <span data-picture data-alt="WK Credentials">
                                <span data-src="images/slide5.jpg" data-media="(min-width: 1601px)"></span>
                            </span>
                            <img src="images/slide5.jpg" id="myImg" alt="" title="" class="gallery" />
                            <p class="flex-caption">Extra Wide Image</p>
                        </li>
                    </ul>
                </div>

                <ul class="flex-direction-nav">
                    <li><a class="prev" href="#"><img src="images/themes.png" alt="" title="" /></a></li>
                    <li><a class="next" href="#"><img src="images/themes.png" alt="" title="" /></a></li>
                </ul>

            </div>

2 个答案:

答案 0 :(得分:0)

试试这个:

$(document).ready(function(){
   $(".slides img").each(function(){
     $(this).load(function(){
        var w = $(this).width();
        var h = $(this).height() *2;

        if(w > h){
          $(this).addClass("auto");
        };
     });
   });
});

答案 1 :(得分:0)

如果班级&#34;画廊&#34;这在实际上意义重大,这可能会有所帮助,否则其他答案都会有效。

$(window).load(function(){
    $(".slides img.gallery").each(function(){
        if($(this).width() > ($(this).height()*2)) {
            $(this).addClass("auto");
        };
    });
});