• 로그인
  • 장바구니에 상품이 없습니다.

home2 게시판 JavaScript, TS 게시판 이미지 기능

이미지 기능

3 글 보임 - 1 에서 3 까지 (총 3 중에서)
  • 글쓴이
  • #16014

    김인환
    참가자

    http://sdcode.dothome.co.kr/sd-main-page/index.html

    이미지 나타내는 기능 만들고 있는데

    코드가 너무 비효율적입니다.. 도와주세여 ㅠㅠ

    function headerFade(){
    $('.header-img-1').css('opacity', 1);
    $('.header-img-2').css('opacity', 0);
    $('.header-img-3').css('opacity', 0);
    $('.header-img-4').css('opacity', 0);
    }
    headerFade();

    setTimeout(function(){
    function headerFade2(){
    $('.header-img-1').css('opacity', 0);
    $('.header-img-2').css('opacity', 1);
    $('.header-img-3').css('opacity', 0);
    $('.header-img-4').css('opacity', 0);
    }
    headerFade2();
    },4000);

    setTimeout(function(){
    function headerFade3(){
    $('.header-img-1').css('opacity', 0);
    $('.header-img-2').css('opacity', 0);
    $('.header-img-3').css('opacity', 1);
    $('.header-img-4').css('opacity', 0);
    }
    headerFade3();
    },8000);

    setTimeout(function(){
    function headerFade4(){
    $('.header-img-1').css('opacity', 0);
    $('.header-img-2').css('opacity', 0);
    $('.header-img-3').css('opacity', 0);
    $('.header-img-4').css('opacity', 1);
    }
    headerFade4();
    },12000);

    setInterval(()=> {
    function headerFade(){
    $('.header-img-1').css('opacity', 1);
    $('.header-img-2').css('opacity', 0);
    $('.header-img-3').css('opacity', 0);
    $('.header-img-4').css('opacity', 0);
    }
    headerFade();

    setTimeout(function(){
    function headerFade2(){
    $('.header-img-1').css('opacity', 0);
    $('.header-img-2').css('opacity', 1);
    $('.header-img-3').css('opacity', 0);
    $('.header-img-4').css('opacity', 0);
    }
    headerFade2();
    },4000);

    setTimeout(function(){
    function headerFade3(){
    $('.header-img-1').css('opacity', 0);
    $('.header-img-2').css('opacity', 0);
    $('.header-img-3').css('opacity', 1);
    $('.header-img-4').css('opacity', 0);
    }
    headerFade3();
    },8000);

    setTimeout(function(){
    function headerFade4(){
    $('.header-img-1').css('opacity', 0);
    $('.header-img-2').css('opacity', 0);
    $('.header-img-3').css('opacity', 0);
    $('.header-img-4').css('opacity', 1);
    }
    headerFade4();
    },12000);
    }, 16000);

    어떻게 짜야지 효율적으로 만들 수 있을까요..?

    #16032

    codingapple
    키 마스터

    함수들이 다 비슷하게 생겼을 땐 

    파라미터 뚫어놓으면 비슷한 함수 여러개 만들 필요 없습니다 

    function headerFade(aaa){

      $('.header-img-1').css('opacity', 0);
      $('.header-img-2').css('opacity', 0);
      $('.header-img-3').css('opacity', 0);
      $('.header-img-4').css('opacity', 0);
      $('.header-img-' + aaa).css('opacity', 1);

    }

    이렇게 만들어두면 headerFade(1) 이러면 .header-img-1 만 보이겠네요

    #16048

    김인환
    참가자

    감사합니다 선생님 ㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠ

3 글 보임 - 1 에서 3 까지 (총 3 중에서)
  • 답변은 로그인 후 가능합니다.

About

현재 월 700명 신규수강중입니다.

  (09:00~20:00) 빠른 상담은 카톡 플러스친구 코딩애플 (링크)
  admin@codingapple.com
  이용약관
ⓒ Codingapple, 강의 예제, 영상 복제 금지
top

© Codingapple, All rights reserved. 슈퍼로켓 에듀케이션 / 서울특별시 강동구 고덕로 19길 30 / 사업자등록번호 : 212-26-14752 온라인 교육학원업 / 통신판매업신고번호 : 제 2017-서울강동-0002 호 / 개인정보관리자 : 박종흠