Arrow function on slide

2

I would like to know if it is possible to make this slide that I adapted from "w3school", work with the arrows, and every time it skips image, the site will rise to the top again.

HTML:

    <!DOCTYPE html>
<html>
<title>slide</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="slide.css"/>
<style>
.mySlides {display:none}
</style>
<body>

    <div class="container">
        <div class="content" style="max-width: 930px">
          <img class="mySlides" src="1.jpg" style="max-width: 100%;display: block;margin-left: auto;margin-right: auto;">
          <img class="mySlides" src="2.jpg" style="max-width: 100%; display: block;margin-left: auto;margin-right: auto;">
          <img class="mySlides" src="3.png" style="max-width: 100%; display: block;margin-left: auto;margin-right: auto;">
        </div>

        <div class="center">
          <div class="section">
            <div class="button" onclick="plusDivs(-1)">❮ Prev</div>
            <div class="button" onclick="plusDivs(1)">Next ❯</div>
          </div><br/>
          <div class="pag demo" onclick="currentDiv(1)">01</div> 
          <div class="pag demo" onclick="currentDiv(2)">02</div> 
          <div class="pag demo" onclick="currentDiv(3)">03</div> 
        </div>
    </div>


<script>
var slideIndex = 1;
showDivs(slideIndex);

function plusDivs(n) {
  showDivs(slideIndex += n);
}

function currentDiv(n) {
  showDivs(slideIndex = n);
}

function showDivs(n) {
  var i;
  var x = document.getElementsByClassName("mySlides");
  var dots = document.getElementsByClassName("demo");
  if (n > x.length) {slideIndex = 1}    
  if (n < 1) {slideIndex = x.length}
  for (i = 0; i < x.length; i++) {
     x[i].style.display = "none";  
  }
  for (i = 0; i < dots.length; i++) {
     dots[i].className = dots[i].className.replace(" red", "");
  }
  x[slideIndex-1].style.display = "block";  
  dots[slideIndex-1].className += " red";
}
</script>

</body>
</html>

CSS:

 .container {
    text-align: center;
}

.content {
    display: block;
    margin-left: auto;
    margin-right: auto;
    width: 1280;
}

.center {
    margin: 0 auto;
    display: inline-block;
    width: auto;
}

.section, code {
    display: inline-block;
    margin-top:16px!important;
    margin-bottom:16px!important;
}

.button {
    border:none;
    display:inline-block;
    padding:8px 16px;
    text-decoration:none;
    background-color: #000;
    color: crimson;
    text-align:center;
    padding:8px 16px;
    margin: 0 auto;
    cursor: pointer;
}

.pag  {
    background: #000;
    color: crimson;
    width: auto;
    display:inline-block;
    padding: 10px;
    cursor: pointer;
}

.red {
    color:#fff!important;
    background-color:#f44336!important;
}

I know a little HTML and CSS, but javascript I do not know anything ...

    
asked by anonymous 07.09.2018 / 21:06

1 answer

1

Change your plusDivs function:

function plusDivs(n) {
      showDivs(slideIndex += n);
      var scrollTop = function() { 
        window.scrollTo(0, 0); 
    };
    }

The window.scrollTo(0, 0) will cause every time the button calling the plusDivs function is clicked the page will scroll to the top!

    
07.09.2018 / 22:05