테스트 사이트 - 개발 중인 베타 버전입니다

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

dkdh12 3년 전 조회 1,890

 $(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개

채택된 답변
+20 포인트
포이치
3년 전

공통 클래스 하나 주시고

예를들어 

 

 

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

아래처럼 될듯한데요.

 

</p>

<p>$(document).ready(function() {</p>

<p> </p>

<p>        $(window).scroll( function(){</p>

<p>            $('.set_class').each( function(){</p>

<p>                var target_cls = $(this).offset().top + $(this).outerHeight() / 5;</p>

<p>                var bottom_of_window = $(window).scrollTop() + $(window).height();</p>

<p>                if( bottom_of_window > target_cls ){</p>

<p>                    $(this).animate({'opacity':'1'},800);</p>

<p>                }</p>

<p>            });</p>

<p> </p>

<p>        });</p>

<p> </p>

<p>    });</p>

<p>

로그인 후 평가할 수 있습니다

답변에 대한 댓글 1개

d
dkdh12
3년 전
오.. 되네요 감사합니다

댓글을 작성하려면 로그인이 필요합니다.

답변을 작성하려면 로그인이 필요합니다.

로그인