jQuery addClass example

Change class name on click in jQuery

by avgustre

HTML

<p></p>
<div>
  <section></section>
  <span class="blck"></span>
  <section></section>
</div>

<a class="left button-left">Влево</a>
<a class="right button-right">Вправо</a>

CSS

p {
  position: fixed;
}

div {
  /* overflow: hidden; */
  position: relative;
  height: 60px;
}

section {}

span {
  position: absolute;
  left: 0;
  display: block;
  height: 50px;
  width: 2050px;
  background: linear-gradient(90deg, red, blue, black);
}

JavaScript

const position = {x: 0, y: 0};

function update(inc) {
	position.x += inc.x || 0;
  
  $('.blck').css({
  	left: position.x + "px",
  });
}

$('.left').on('click', function(){
update({x: -100});
});
$('.right').on('click', function(){
update({x: 100});
});