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 7783131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:45:05+00:00 2026-06-01T19:45:05+00:00

I have an image gallery which works great. When the last slide is reached

  • 0

I have an image gallery which works great. When the last slide is reached the image gallery just shuffles back to the first image.

Now, what I want to know is how can I disable the next or previous image when the last/first image has been reached? My code is…

Slider Script

function Slider(container, nav) {

    this.container = container;
    this.nav = nav.show();

    this.imgs = this.container.find('img');
    this.imgWidth = this.imgs[0].width;
    this.imgsLen = this.imgs.length;

    this.current = 0; 

}

Slider.prototype.transition = function ( coords ) {
    this.container.animate({
        'margin-left': coords || -( this.current * this.imgWidth ) 
    });
};

Slider.prototype.setCurrent = function( dir ) {
    var pos = this.current;
    pos += ( ~~( dir === 'next' ) || -1); 
    this.current = ( pos < 0  ) ? this.imgsLen  - 1 : pos % this.imgsLen; 
    return pos;
}; 

HTML

<div class="slider">
    <ul>
        <li>
            <img src="http://..../spacer.gif" height="400" />
        </li>
        <li>
            <img src="http://..../spacer.gif" height="400" />
        </li>
    </ul>
</div>

<div id="slider-nav">
    <button class="previous" data-dir="prev">Previous</button>
    <button data-dir="next">Next</button>
</div>​

Slider Initiation

<script>
    var container = $('div.slider').css('overflow', 'hidden').children('ul');

    var slider = new Slider( container, $('#slider-nav') );

        slider.nav.find('button').on('click', function() {
        slider.setCurrent( $(this).data('dir') );
        slider.transition();
    });
</script>

I understand this is probably an arse-about-face way of going about building a slider, but I’ve not been working with jQuery for long.

I’ve tried doing something along the lines of..

if (this.current = this.imgsLen - 1) {
    $(':button:contains("previous")').attr('disabled', 'disabled');
}

But to no avail. I’ve googled this too, and still haven’t come up with anything which has worked.

In case you need one to play about with, heres a jsFiddle of it (ignore the fact that the image doesnt slide automatically, this is because of the li having a border!)

  • 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-06-01T19:45:06+00:00Added an answer on June 1, 2026 at 7:45 pm

    Ok, so you’ve got a few things going on. In this code, there are two problems

    if (this.current = this.imgsLen - 1) {
        $(':button:contains("previous")').attr('disabled', 'disabled');
    }
    

    First, assignment. this.current = this.imgsLen - 1. Notice the single =? You’re setting this.current whenever the code runs.

    Second, :button is shorthand for $("button, input[type='button']"). It’ll work but since you know you only have <botton> you might just as well use $('button') since it will be faster.

    Finally, the code previously submitted, but here’s another option. Change up your nav to have previous and next classes:

    <div id="slider-nav">
        <button class="previous" data-dir="prev">Previous</button>
        <button class="next" data-dir="next">Next</button>
    </div>​
    

    Then add this to your transition function:

    if (this.current == this.imgsLen - 1) {
        console.log('disable next')
        $('button.next').addClass('disabled').attr('disabled', true)
        $('button.previous').removeClass('disabled').attr('disabled', false)
    }
    else if(this.current == 0) {
        console.log('disable previous')
        $('button.next').removeClass('disabled').attr('disabled', false)
        $('button.previous').addClass('disabled').attr('disabled', true)
    }
    else {
        console.log('enable all')
        $('button.next').removeClass('disabled').attr('disabled', false)
        $('button.previous').removeClass('disabled').attr('disabled', false)
    }
    

    Here’s a working version:

    http://jsfiddle.net/Qqk3U/15/

    Hope that helps.

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

Sidebar

Related Questions

what i want to do is that i have a nice image Gallery which
I have created a XML image gallery, which displays text in between each slide.
I have built a really simple image fading gallery sorta thing, which works on
I have an Activity which is used as an image gallery and works quite
Hi I have a gallery in my app which shows the image in an
I have a image gallery like Google+ or Facebook and i want to avoid
I have a simple image gallery here . All I want to do is
I have made a image gallery with jQuery found here: http://sarfraznawaz2005.kodingen.com/demos/jquery/image_gallery/ I just wanted
I have an image gallery which loads a detail image, then draws a line
We have an ashx image handler which has performed fairly well over the last

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.