Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get img by its css property jQuery

Tags:

jquery

I want to get an img by its css property, on jQuery. the property is margin-left:-1920px.

How can I do that?

I tried $('img').css('margin-left', '-1920px'); console.log('img'); but of course its changing the property and this I don't want. I want to get the element first and then changing the property. How can I get the element?

my jQuery code:

$(document).ready(function() {
var sliderUL = $('.slider').children('ul'),
    imgs = sliderUL.find('img'),
    imgWidth = imgs[0].width, // 960
    imgsLen = imgs.length,  // 4
    current = 1,
    totalImgsWidth = imgsLen * imgWidth;


$('#slider-nav')
    .show().find('button').on('click', function() {
        direction = $(this).data('dir');

    var loc = imgWidth;
        (direction === 'next') ? ++current : --current;

        if (current === 0) {
            current = imgsLen;
            direction = 'next';
            loc = totalImgsWidth - imgWidth;


        } else if ( current - 1 === imgsLen ) {
            current = 1;
            loc = 0;
        }

        transition(sliderUL, loc, direction);


    });
function transition( container, loc, direction ) {
    var unit;

    if ( direction && loc !== 0 ) {
        unit = ( direction === 'next' ) ? '-=' : '+=';
    }

    container.animate({
        'margin-left': unit ? (unit+loc) : loc
    })

}

});

my HTML:

<body>
<div id="slider-nav">
    <button data-dir="prev"><</button>
    <button data-dir="next">></button>
</div>
<div class="slider">
    <ul>
        <li><img src="img1.jpg" alt="image"></li>
        <li><img src="img2.jpg" alt="image"></li>
        <li><img src="img3.jpg" alt="image"></li>
        <li><img src="img4.jpg" alt="image"></li>
    </ul>
</div>

like image 319
Mor Baruch Avatar asked Feb 13 '23 18:02

Mor Baruch


2 Answers

You can filter those images with that style using filter():

var img = $('img').filter(function() {
    return $(this).css('margin-left') == '-1920px';
});
like image 154
Felix Avatar answered Feb 17 '23 01:02

Felix


You can use:

 var x = $('img').filter(function () { 
   return this.style.marginLeft == '-1920px' 
});
like image 31
Milind Anantwar Avatar answered Feb 17 '23 03:02

Milind Anantwar