David
David

Reputation: 85

JS getting width not working as intended

So, im having some issues with my web; I've got a responsive image slider that works properly if I give him a numeric value, so I made a JS to get this value when i load the web. This is the js :

<script>
    $(document).ready(function(){
        $('.item').css({
            'width': w + 'px'
        });
    });
</script>

The problem is that it gets the value but dont work as intended.

Here is an img of how it works with the numeric value on css :

working as intended And this is what it does when it gets the value with the js.

Not working properly.

Any help would be appreciated. Thanks!

P.S. Im using iosslider, cant put the web cause of permissions.

Upvotes: 0

Views: 204

Answers (1)

Shintu Joseph
Shintu Joseph

Reputation: 962

Calculating the width of the image on document.ready may give unfavourabe results, try the width calculation on windows load event or if feasible to add a vendor JS use images loaded plugin which will watch images load to perfection.

To be more specific,

$('.item img').on('load',function(){
    //width calculation here
});

Upvotes: 2

Related Questions