JS 이미지 전환 효과 구현

2098 단어 js그림 전환
본 논문 의 사례 는 JS 가 이미지 전환 을 실현 하 는 구체 적 인 코드 를 공유 하여 여러분 께 참고 하 시기 바 랍 니 다.구체 적 인 내용 은 다음 과 같 습 니 다.
지식 포인트:
1.window.onload 웹 페이지 를 모두 불 러 온 후 실행 합 니 다.
2.원소 설정 속성 가 져 오기
3.임계 상황 판단
실행 효과:


이전 다음 장 을 클릭 하여 그림 전환
코드:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<style>
  #box{
    width: 1200px;
    margin: 0 auto;
  }
</style>
<body>
  <div id="box">
    <img id="icon" src="images/  1.gif" alt="">
    <p></p>
    <button id="prep">   </button>
    <button id="next">   </button>
  </div>
<script>
  window.onload = function (ev) {
    // 1.     
    var prep = document.getElementById('prep');
    var next = document.getElementById('next');
    var icon = document.getElementById('icon');
    // 2.   
    var currentIndex = 1, minIndex=1, maxIndex=10;
    prep.onclick = function (ev1) {
      if (currentIndex === minIndex){
        currentIndex = maxIndex;
      }else{
        currentIndex--;
      }
      icon.setAttribute('src','images/  '+ currentIndex +'.gif');
    };
    next.onclick = function (ev1) {
      if (currentIndex === maxIndex){
        currentIndex = minIndex;
      }else {
        currentIndex++;
      }
      icon.setAttribute('src','images/  '+ currentIndex +'.gif');
    }
  }
</script>
</body>
</html>
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기