본문 바로가기
Coding/JavaScript

[JavaScript & jQuery] html에서 이미지 변경 시 사라졌다 나타나는 효과(fadeIn & fadeOut)

by 포스트it 2023. 6. 8.
반응형

 

[JavaScript & jQuery] html에서 이미지 변경 시 사라졌다 나타나는 효과(fadeIn & fadeOut)

이미지 변경시킬 때 페이드인 이나 페이드아웃 효과 주는 방법 입니다 !

 

예제코드
<!doctype html>
<html lang="ko">
  <head>
    <meta charset="utf-8">
    <title>fadeInOut</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

  </head>
  <body>
    <button>클릭!!</button>
    <img style="width: 25%;" src="https://cdn.autotribune.co.kr/news/photo/202304/8017_43246_1529.jpg">
  </body>
</html>
<script>
$(function(){
  $("button").click(function(){
    $("img").fadeOut(2000, function(){
      $("img").attr("src", "https://file2.nocutnews.co.kr/newsroom/image/2023/05/03/202305031714287586_0.jpg");
      $("img").fadeIn(2000);
    });
  });
});
</script>

결과값

728x90
반응형

댓글