我的任务是在滚动时淡化元素,就像这样 - http://layot.prestatrend.com/ 我已经从两个来源收集的jQuery代码的帮助下做到了:
<script type="text/javascript">
$(document).ready( function() {
tiles = $('.ajax_block_product').fadeTo(0,0);
$(window).scroll(function(d,h) {
tiles.each(function(i) {
a = $(this).offset().top + $(this).height();
b = $(window).scrollTop() + $(window).height();
if (a < b) $(this).fadeTo(500,1);
});
});
function inWindow(s){
var scrollTop = $(window).scrollTop();
var windowHeight = $(window).height();
var currentEls = $(s);
var result = [];
currentEls.each(function(){
var el = $(this);
var offset = el.offset();
if(scrollTop <= offset.top && (el.height() + offset.top) < (scrollTop + windowHeight))
result.push(this);
});
return $(result);
}
inWindow('.ajax_block_product').fadeTo(0,1);
});
</script>
这是一个优雅的解决方案还是有办法让代码更短更优雅?