6

I am trying to get this horizontal section of the page to auto scroll when the left or right arrows are clicked. I can get the Jquery code to run correctly in the console. However, the auto scroll events won't run at all on my page. Could anyone provide any insight into this issue?

The code is as follows :

HTML

<div class = "horizon horizon-prev">
      <img src = "../images/left-arrow.png" />
    </div>
    <div class = "horizon horizon-next">
      <img src = "../images/right-arrow.png" />
    </div>

  <div class="center" id="content">
      <div class=internal>
        div 1
      </div>
       <div class=internal>
        div 2
      </div>
       <div class=internal>
        div 3
      </div>
       <div class=internal>
        div 4
      </div>
       <div class=internal>
        div 5
      </div>
       <div class=internal>
        div 6
      </div>
       <div class=internal>
        div 7
      </div>
       <div class=internal>
        div 8
      </div>
    </div>

CSS

div.center {
 width: 90%;
 height: 210px;
 border: 1px solid #000;
 margin: auto;
 overflow-x: hidden;
 overflow-y: hidden;
 white-space: nowrap;
  }
  div.internal {
   display: inline-block;
   vertical-align: middle;
   width: 100%;
   text-align: center;
  }

Jquery

$('.horizon-prev').click(function() {
  event.preventDefault();
  $('#content').animate({
    scrollLeft: "-=775px"
  }, "slow");
});

 $('.horizon-next').click(function() {
  event.preventDefault();
  $('#content').animate({
   scrollLeft: "+=775px"
  }, "slow");
});
8
  • Where are .horizon-prev and .horizon-next class? Commented May 26, 2016 at 14:12
  • @Error404 I've just edited the example to show where they are. Sorry for the lack of code. Commented May 26, 2016 at 14:14
  • jsbin.com/wokofepuvi/1/edit?html,css,js,output It works for me Commented May 26, 2016 at 14:18
  • As @VladGincher points this works ... Also I suggest you since you have % width to take the Scroll value from the actual width of center ... Check this jsfiddle.net/fwnv28hu Commented May 26, 2016 at 14:25
  • @VladGincher check out sethspivey.com. For some reason, it's only working when I run the code in the console. Commented May 26, 2016 at 14:26

5 Answers 5

5

(based on looking at your site sethspivey.com)

You need to add the missing event parameter you are using, and also move the click handlers into document.ready, like so:

$(function() {
  $('.horizon-prev').click(function(event) {
    event.preventDefault();
    $('#content').animate({
      scrollLeft: "-=775px"
    }, "slow");
  });

   $('.horizon-next').click(function(event) {
    event.preventDefault();
    $('#content').animate({
     scrollLeft: "+=775px"
    }, "slow");
  });
});
Sign up to request clarification or add additional context in comments.

Comments

3

I take your code as this, and it work perfectly.

$('.horizon-prev').click(function() {
  event.preventDefault();
  $('#content').animate({
    scrollLeft: "-=775px"
  }, "slow");
});

 $('.horizon-next').click(function() {
  event.preventDefault();
  $('#content').animate({
   scrollLeft: "+=775px"
  }, "slow");
});
div.center {
 width: 90%;
 height: 210px;
 border: 1px solid #000;
 margin: auto;
 overflow-x: hidden;
 overflow-y: hidden;
 white-space: nowrap;
  }
  div.internal {
   display: inline-block;
   vertical-align: middle;
   width: 100%;
   text-align: center;
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="center" id="content">
      <div class=internal>
        div 1
      </div>
       <div class=internal>
        div 2
      </div>
       <div class=internal>
        div 3
      </div>
       <div class=internal>
        div 4
      </div>
       <div class=internal>
        div 5
      </div>
       <div class=internal>
        div 6
      </div>
       <div class=internal>
        div 7
      </div>
       <div class=internal>
        div 8
      </div>
    </div>
    
    <button class="horizon-prev">Prev</button>    <button class="horizon-next">Next</button>

Comments

1

The problem is that jQuery binded an event handler to the "click" JavaScript event on a div that isn't loaded yet. You should execute it only when the DOM is fully loaded.

To fix it, in the http://sethspivey.com/js/index.js file, change the code to:

/* CLICK SCROLL FUNCTIONS JQUERY */
$(function(){
  $('.horizon-prev').click(function(event) {
    event.preventDefault();
    $('#content').animate({
      scrollLeft: "-="+toScroll+"px"
    }, "slow");
  });

  $('.horizon-next').click(function(event) {
    event.preventDefault();
    $('#content').animate({
      scrollLeft: "+="+toScroll+"px"
    }, "slow");
  });
});

3 Comments

Works like a charm after combining this and defining the scrollTo that DaniP provided in his jsfiddle. Thank you
Re-edit your answer ... since you are using the code I provided on the comments please copy the complete code...Here is no toScroll value
@DaniP I used the code in sethspivey.com/js/index.js line 217, all I've done is adding $(function(){ at the beginning and }); at the end...
1

this is an old post and maybe there is an easier way to achive this now, but nevertheless it helped me with a project I am working on now. This scrollLeft recommendation above works a treat, but when you get to the end of the div if you continue clicking right 5 times for example, it adds 5 x 200px to scrollLeft, so you have to click left 6 times to move back 200px. This additional code solved the issue for me. Alternatively you could disable the button when reaching the end/beginning of the content div:

$('.horizon-prev').click(function() {
  event.preventDefault();
  let currentScroll = $('#content').get(0).scrollLeft;
  let scrollWidth = $('#content').get(0).scrollWidth;
  if((currentScroll - 200) <= 0){
        return;
  }else{
        $('#content').animate({
            scrollLeft: '-=200px'
        }, "slow");
  }
});


$('.horizon-next').click(function() {
  event.preventDefault();
  let currentScroll = $('#content').get(0).scrollLeft;
  let scrollWidth = $('#content').get(0).scrollWidth;
  if((200 + currentScroll) >= scrollWidth){
        return;
  }else{
        $('#content').animate({
            scrollLeft: '+=200px'
        }, "slow");
  }
});

2 Comments

Works great, thanks, but I think you have two next selectors instead of next and prev
Indeed I did. Updated this now thanks.
0

With some modifications for better demonstration seems to me working fine:

<div class="horizon horizon-prev">
  <img src="../images/left-arrow.png" />
</div>
<div class="horizon horizon-next">
  <img src="../images/right-arrow.png" />
</div>

<div class="center" id="content">
  <div class=internal>
    div 1
  </div>
  <div class=internal>
    div 2
  </div>
  <div class=internal>
    div 3
  </div>
  <div class=internal>
    div 4
  </div>
  <div class=internal>
    div 5
  </div>
  <div class=internal>
    div 6
  </div>
  <div class=internal>
    div 7
  </div>
  <div class=internal>
    div 8
  </div>

div.center {
  width: 200px;
  height: 210px;
  border: 1px solid #000;
  margin: auto;
  overflow-x: hidden;
  overflow-y: hidden;
  white-space: nowrap;
}

div.internal {
  margin: -2px;
  display: inline-block;
  vertical-align: middle;
  width: 198px;
  text-align: center;
}

$('.horizon-prev').click(function() {
  event.preventDefault();
  $('#content').animate({
    scrollLeft: "-=198px"
  }, "slow");
});

$('.horizon-next').click(function() {
  event.preventDefault();
  $('#content').animate({
    scrollLeft: "+=198px"
  }, "slow");
});

https://jsfiddle.net/w87xycrq/1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.