Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7637623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:59:18+00:00 2026-05-31T07:59:18+00:00

I created a slider with 2 buttons, previous and next, allowing the user to

  • 0

I created a slider with 2 buttons, previous and next, allowing the user to interact with images by going forward or back. If at the first image and previous is fired, the last image is slid left into view. Opposite goes for the end of images case.

The problem I’m having is getting the width of the static images dynamically. When I use the browser console with the exact same selector and methods, I get the correct width, but when i console.log(width), the value is 0, which throws off the animation to slide 0px, hence no interaction.

Here is my JS:

$(document).ready(function () {

//initialize/cache variables needed in program
var sliderUL = $('div.content5').css('overflow', 'hidden').children('ul');          //we can chain variables, for example: we are setting the overflow of an element to hidden, and then we are getting the selected elements of 'ul'.
var imgs = sliderUL.find('img');
var imgWidth = imgs.eq(0).width();           //*SHOULD BE 400, but is 0*
var imgsLen = imgs.length;                  //5
var current = 1;
var totalImgsWidth = imgsLen * imgWidth;   //2000

$('#slider-nav').show().find('button').on('click', function () {

    //direction tells us what button is clicked, previous or next. This is needed for moving forward or backwards
    var direction = $(this).data('dir'),
        loc = imgWidth;     //400, the amount needed to shift to new image, which also is the width of an image

    //update current value
    //if the user clicks on the next button, increase current by 1. else, decrease current by 1, meaning they clicked on the previous button.
    (direction === 'next') ? ++current : --current;
    //if on the first image and the user presses previous, set current equal to the last image
    if (current === 0) {
        current = imgsLen;
        loc = totalImgsWidth - imgWidth;
        //direction next means that it will animate left all the way over to the last image
        direction = 'next';
    } else if (current - 1 === imgsLen) {
        //if the user is on the very last image and they press next, send them back to the first image
        current = 1;
        //send the img location back to 0 pixels, or the first image
        loc = 0;
    }
    transition(sliderUL, loc, direction);
});

//params: 1.what we are animating 2.the location (margin) we're moving to 3.the direction we are moving
function transition(container, loc, direction) {

    var unit;   // -=  OR  +=
    //as long as the user isn't trying to reset, the unit is either going to be equal to -= or +=
    console.log(loc + ' and ' + direction);
    if (direction && loc !== 0) {
        //does direction equal next?
        //if so, increment left sign, else, increment right sign
        if (direction == 'next') {
            unit = '-=';
        } else {
            unit = '+=';
        }
        //            unit = (direction === 'next') ? '-=' : '+=';
    }
    console.log("you are on image: " + current + ", going " + unit);
    container.animate({
        //if unit isn't undefined, animate container. else, reset to 0/first img
        'margin-left': unit ? (direction + loc) : loc    // if true, -=400  OR  +=400   if false, reset back to first image at 0
    });
  }
});

Here is my HTML:

<div class="content5">
    <h1 class="content5_h1">Content #5 - The Obligatory Slider</h1>
    <ul class="content5_imgs">
        <li><img src="imgs/img1.jpg" alt="/" /></li>
        <li><img src="imgs/img2.jpg" alt="/" /></li>
        <li><img src="imgs/img3.jpg" alt="/" /></li>
        <li><img src="imgs/img4.jpg" alt="/" /></li>
        <li><img src="imgs/img5.jpg" alt="/" /></li>
    </ul>
</div>
<div id="slider-nav">
    <button data-dir="prev">Previous</button>
    <button data-dir="next">Next</button>
</div>

and finally, here is my CSS:

.content5 {
    width:400px;
    margin:auto;
    height: 368px;
    overflow: scroll;
}

.content5_imgs {
    width:10000px;
    height: 250px;
    list-style: none;
    padding: 0px;
}

.content5_imgs li {
    float:left;
}

#slider-nav {
    display: none;
    margin-top: 1em;
    width:170px;
    margin:auto;
    padding-top: 10px;
}

#slider-nav button {
    padding:1em;
    margin-right: 1em;
    border-radius:10px;
    cursor: pointer;
    background-color: Lime;
}

I tried chaining the variables, and I tried even changing the selectors for imgWidth including first() and [0].

Any insight is helpful.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T07:59:19+00:00Added an answer on May 31, 2026 at 7:59 am

    JavaScript will return a width of 0 if the image hasn’t loaded yet.

    To work around this, any code that depends on content to be loaded, rather than just the DOM being ready, needs to be called on

    $(window).load()
    

    Instead of $(document).ready().

    More information: Official way to ask jQuery wait for all images to load before executing something

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a jquery slider. On clicking next or back buttons .animate function
I need a to create a slide show with previous and next buttons. So
IBOutlets are for buttons, labels, images, slider, pickerview and all other controls we drag
I created an slider that slide all images of my wordpress articles, and if
I have a page which contains a jQuery-UI horizontal slider, created using a little
I have created a XML image gallery, which displays text in between each slide.
Ok, I tried to create another slider of preloaded images (whatever you wanna call
I want to create a slider with images like a control in the bottom
I've created a back to home type button in Android by using the code:
I've created a 2d slider in jQuery in which 2 parameters are manipulated simultaneously

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.