2013-03-23 37 views
8

Ví dụ: http://www.chartjs.org/Fade trong phần tử trên cuộn xuống bằng css

Khi di chuyển xuống mỗi bài viết xuất hiện khi nó được trong giao diện cổng của trình duyệt. Css là

#examples article { 
    -webkit-transition: opacity 200ms ease-in-out; 
    -ms-transition: opacity 200ms ease-in-out; 
    -moz-transition: opacity 200ms ease-in-out; 
    -o-transition: opacity 200ms ease-in-out; 
    transition: opacity 200ms ease-in-out; 
    position: relative; 
    margin-top: 20px; 
    clear: both; 
} 

Tôi đã thử css nhưng không hoạt động. Có một số javascript mà làm việc cùng với css? Làm thế nào tôi có thể đạt được loại scroll-> fadeIn có hiệu lực?

+2

Hãy thử với [JQuery Waypoints] (http://imakewebthings.com/ jquery-waypoints /). – Vucko

Trả lời

23

Demo Fiddle

Bạn có muốn một cái gì đó như thế này?

$(window).scroll(function() { 

     /* Check the location of each desired element */ 
     $('.article').each(function (i) { 

      var bottom_of_object = $(this).position().top + $(this).outerHeight(); 
      var bottom_of_window = $(window).scrollTop() + $(window).height(); 

      /* If the object is completely visible in the window, fade it it */ 
      if (bottom_of_window > bottom_of_object) { 

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

      } 

     }); 

    }); 
+4

Tại sao xác định "i" nhưng không bao giờ sử dụng? –

+0

Tại sao mọi người nếu tôi di chuyển lên và xuống tại một thời điểm sự cố hiệu ứng? –

3

Mohammeds câu trả lời không đưa vào tài khoản bất kỳ hình ảnh hoàn toàn vị trí ... chúng ta nên sử dụng bù đắp thay vì vị trí

$(window).scroll(function() { 

    /* Check the location of each desired element */ 
    $('.article').each(function (i) { 

     var bottom_of_object = $(this).offset().top + $(this).outerHeight(); 
     var bottom_of_window = $(window).scrollTop() + $(window).height(); 

     /* If the object is completely visible in the window, fade it it */ 
     if (bottom_of_window > bottom_of_object) { 

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

     } 
    }); 
}); 
Các vấn đề liên quan