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

  • Home
  • SEARCH
  • 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 808783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:37:10+00:00 2026-05-15T00:37:10+00:00

I need to make a slider. I have content (which should shift horizontally) and

  • 0

I need to make a slider.

I have content (which should shift horizontally) and two buttons – “right” and “left”.

If you press the button and hold it, the content starts to move (in the appropriate direction). If you not hold the button, then the movement stops. This behavior copies the behavior of a usual window scrollbar.

I wrote some code:

var animateTime = 1,
    offsetStep = 5;

//event handling for buttons "left", "right"
$('.bttR, .bttL')
    .mousedown(function() {
        scrollContent.data('loop', true).loopingAnimation($(this));
    })
    .bind("mouseup mouseout", function(){
        scrollContent.data('loop', false);
    });


$.fn.loopingAnimation = function(el){
    if(this.data('loop') == true){
        var leftOffsetStr;

        leftOffsetInt = parseInt(this.css('marginLeft').slice(0, -2));

        if(el.attr('class') == 'bttR')
            leftOffsetStr = (leftOffsetInt - offsetStep).toString() + 'px';
        else if(el.attr('class') == 'bttL')
            leftOffsetStr = (leftOffsetInt + offsetStep).toString() + 'px';


        this.animate({marginLeft: leftOffsetStr}, animateTime, function(){
            $(this).loopingAnimation(el);
        })
    }
    return this;
}

But it does have a few things that I do not like:

  1. It always calls the function animation (loopingAnimation) – I think that this is an extra load (not good).
  2. When moving content he “twitches and trembling” – (it’s not pretty).

How can I solve this problem more elegantly and without the drawbacks of my code?

  • 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-15T00:37:11+00:00Added an answer on May 15, 2026 at 12:37 am

    I don’t think you can get around looping through the function or the twitching and trembling if you animate more than one pixel at a time.

    But I did try to clean up your code a bit,because you can use +=1px or -=1px with the animation function: (Update: removed the animation function, but you can use +=1px or -=1px for future reference)

    $(document).ready(function(){
    
        var animateTime = 1,
             offsetStep = 5,
             scrollWrapper = $('#wrap');
    
     //event handling for buttons "left", "right"
     $('.bttR, .bttL')
        .mousedown(function() {
            scrollWrapper.data('loop', true);
            loopingAnimation($(this), $(this).is('.bttR') );
        })
        .bind("mouseup mouseout", function(){
            scrollWrapper.data('loop', false).stop();
            $(this).data('scrollLeft', this.scrollLeft);
        });
    
        loopingAnimation = function(el, dir){
            if(scrollWrapper.data('loop')){
                var sign = (dir) ? offsetStep : -offsetStep;
                scrollWrapper[0].scrollLeft += sign;
                setTimeout( function(){ loopingAnimation(el,dir) }, animateTime );
            }
            return false;
        };
    
    })
    

    Because I have OCD and don’t like slow scrollers, I made a demo with mousewheel functionality and mouse drag and drop functionality. Here is the extra code:

    Update: Actually, I think if you don’t use the jQuery animate function you get a smoother scroll. I have updated the code above, and the demo.

    $('#wrap')  // wrap around content
        .mousedown(function(event) {
            $(this)
                .data('down', true)
                .data('x', event.clientX)
                .data('scrollLeft', this.scrollLeft);
            return false;
        })
        .mouseup(function (event) {
            $(this).data('down', false);
        })
        .mousemove(function (event) {
            if ($(this).data('down') == true) {
                this.scrollLeft = $(this).data('scrollLeft') + $(this).data('x') - event.clientX;
            }
        })
        .mousewheel(function (event, delta) {
            this.scrollLeft -= (delta * 30);
        })
        .css({
            'overflow' : 'hidden',
            'cursor' : '-moz-grab'
        });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to make a four posts featured content slider but not with the
I have a crystal report file I need make a tiny edit in. It
I need to make an ArrayList of ArrayLists thread safe. I also cannot have
I have this website which has a slider in the middle of the page.
I need make a GET call to a REST api which is rate limited.
I need make UIAlertView blocking. Because i have function and i need to return
i have a input tag which is non editable, but some times i need
I have several div tags containing simple html/css content and I need a simple
I have found exactly what I need on the jQuery UI examples: Collapsable Content
I have a free Cu3r Slider and I need to embed a font of

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.