해당코드 더 짧게 할 수 있는 방법 있을까요?

해당코드 더 짧게 할 수 있는 방법 있을까요?

QA

해당코드 더 짧게 할 수 있는 방법 있을까요?

본문

 $(document).ready(function() {

        $(window).scroll( function(){

               

            $('.mb-pick-wrap').each( function(){

               

                var mb_pick_wrap = $(this).offset().top + $(this).outerHeight() / 5;

                var bottom_of_window = $(window).scrollTop() + $(window).height();

               

                if( bottom_of_window > mb_pick_wrap ){

                    $(this).animate({'opacity':'1'},800);

                }

               

            });

 

            $('.Dr_fixed_Bg').each( function(){

               

                var Dr_fixed_Bg = $(this).offset().top + $(this).outerHeight() / 5;

                var bottom_of_window = $(window).scrollTop() + $(window).height();

               

                if( bottom_of_window > Dr_fixed_Bg ){

                    $(this).animate({'opacity':'1'},800);

                }

               

            });

 

            $('.other-site-move').each( function(){

               

                var other_site_move = $(this).offset().top + $(this).outerHeight() / 5;

                var bottom_of_window = $(window).scrollTop() + $(window).height();

               

                if( bottom_of_window > other_site_move ){

                    $(this).animate({'opacity':'1'},800);

                }

               

            });

        });

    });

 

클래스명만 다를뿐 나머지 효과는 같은데 더 코드를 짧게 할 수 있는 방법 있을까요?

이 질문에 댓글 쓰기 :

답변 1

공통 클래스 하나 주시고

예를들어 

 

<div class='mb-pick-wrap set_class'></div>

<div class='Dr_fixed_Bg set_class'></div>

<div class='other-site-move set_class'></div>

 

이런식으로 set_class 라는 공통 클래스를 선언하면

아래처럼 될듯한데요.

 


$(document).ready(function() {
 
        $(window).scroll( function(){
            $('.set_class').each( function(){
                var target_cls = $(this).offset().top + $(this).outerHeight() / 5;
                var bottom_of_window = $(window).scrollTop() + $(window).height();
                if( bottom_of_window > target_cls ){
                    $(this).animate({'opacity':'1'},800);
                }
            });
 
        });
 
    });
답변을 작성하시기 전에 로그인 해주세요.
전체 1,013
QA 내용 검색

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1404호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT