Home  >  Article  >  Backend Development  >  PHP+Jquery combined with ajax to achieve drop-down and fade-out waterfall flow effect [no plug-in required]

PHP+Jquery combined with ajax to achieve drop-down and fade-out waterfall flow effect [no plug-in required]

高洛峰
高洛峰Original
2017-01-03 09:58:07929browse

Introduction:

Waterfall flow, also known as waterfall flow layout. It is a popular website page layout. The visual performance is a jagged multi-column layout. As the page scroll bar scrolls down, this layout will continuously load data blocks and append them to the current tail. The first website to adopt this layout was Pinterest, which gradually became popular in China. Most domestic fresh websites are basically of this style, such as Huaban.com, Mogujie, Meilishuo, etc.

No nonsense, just go to the code. The entire code is divided into two sections of code. The specific code is as follows.

Front desk

$category=$this->getMyVal('category',$_GET);
$xiaohuaList=Xiaohua::model()->getXiaohao($category); //打开页面默认显示的数据
?>

id;?>
title);?>
content;?>
分享

Backstage:

public function actionListXiaohua() {
$xiaohuaList=Xiaohua::model()->getXiaohua();//获取笑话信息
echo CJSON::encode($xiaohuaList);
}

js:

   
;(function($){
var
//参数
setting={
column_width:240,//列宽
column_className:'waterfall_column',//列的类名
column_space:2,//列间距
cell_selector:'.cell',//要排列的砖块的选择器,context为整个外部容器
img_selector:'img',//要加载的图片的选择器
auto_imgHeight:true,//是否需要自动计算图片的高度
fadein:true,//是否渐显载入
fadein_speed:600,//渐显速率,单位毫秒
insert_type:1, //单元格插入方式,1为插入最短那列,2为按序轮流插入
getResource:function(index){ } //获取动态资源函数,必须返回一个砖块元素集合,传入参数为加载的次数
},
//
waterfall=$.waterfall={},//对外信息对象
$waterfall=null;//容器
waterfall.load_index=0, //加载次数
$.fn.extend({
waterfall:function(opt){
opt=opt||{};
setting=$.extend(setting,opt);
$waterfall=waterfall.$waterfall=$(this);
waterfall.$columns=creatColumn();
render($(this).find(setting.cell_selector).detach(),false); //重排已存在元素时强制不渐显
waterfall._scrollTimer2=null;
$(window).bind('scroll',function(){
clearTimeout(waterfall._scrollTimer2);
waterfall._scrollTimer2=setTimeout(onScroll,300);
});
waterfall._scrollTimer3=null;
$(window).bind('resize',function(){
clearTimeout(waterfall._scrollTimer3);
waterfall._scrollTimer3=setTimeout(onResize,300);
});
}
});
function creatColumn(){//创建列
waterfall.column_num=calculateColumns();//列数
//循环创建列
var html='';
for(var i=0;i
'; } $waterfall.prepend(html);//插入列 return $('.'+setting.column_className,$waterfall);//列集合 } function calculateColumns(){//计算需要的列数 var num=Math.floor(($waterfall.innerWidth())/(setting.column_width+setting.column_space)); if(num<1){ num=1; } //保证至少有一列 return num; } function render(elements,fadein){//渲染元素 if(!$(elements).length) return;//没有元素 var $columns = waterfall.$columns; $(elements).each(function(i){ if(!setting.auto_imgHeight||setting.insert_type==2){//如果给出了图片高度,或者是按顺序插入,则不必等图片加载完就能计算列的高度了 if(setting.insert_type==1){ insert($(elements).eq(i),setting.fadein&&fadein);//插入元素 }else if(setting.insert_type==2){ insert2($(elements).eq(i),i,setting.fadein&&fadein);//插入元素 } return true;//continue } if($(this)[0].nodeName.toLowerCase()=='img'||$(this).find(setting.img_selector).length>0){//本身是图片或含有图片 var image=new Image; var src=$(this)[0].nodeName.toLowerCase()=='img'?$(this).attr('src'):$(this).find(setting.img_selector).attr('src'); image.onload=function(){//图片加载后才能自动计算出尺寸 image.onreadystatechange=null; if(setting.insert_type==1){ insert($(elements).eq(i),setting.fadein&&fadein);//插入元素 }else if(setting.insert_type==2){ insert2($(elements).eq(i),i,setting.fadein&&fadein);//插入元素 } image=null; } image.onreadystatechange=function(){//处理IE等浏览器的缓存问题:图片缓存后不会再触发onload事件 if(image.readyState == "complete"){ image.onload=null; if(setting.insert_type==1){ insert($(elements).eq(i),setting.fadein&&fadein);//插入元素 }else if(setting.insert_type==2){ insert2($(elements).eq(i),i,setting.fadein&&fadein);//插入元素 } image=null; } } image.src=src; }else{//不用考虑图片加载 if(setting.insert_type==1){ insert($(elements).eq(i),setting.fadein&&fadein);//插入元素 }else if(setting.insert_type==2){ insert2($(elements).eq(i),i,setting.fadein&&fadein);//插入元素 } } }); } function public_render(elems){//ajax得到元素的渲染接口 render(elems,true); } function insert($element,fadein){//把元素插入最短列 if(fadein){//渐显 $element.css('opacity',0).appendTo(waterfall.$columns.eq(calculateLowest())).fadeTo(setting.fadein_speed,1); }else{//不渐显 $element.appendTo(waterfall.$columns.eq(calculateLowest())); } } function insert2($element,i,fadein){//按序轮流插入元素 if(fadein){//渐显 $element.css('opacity',0).appendTo(waterfall.$columns.eq(i%waterfall.column_num)).fadeTo(setting.fadein_speed,1); }else{//不渐显 $element.appendTo(waterfall.$columns.eq(i%waterfall.column_num)); } } function calculateLowest(){//计算最短的那列的索引 var min=waterfall.$columns.eq(0).outerHeight(),min_key=0; waterfall.$columns.each(function(i){ if($(this).outerHeight()=bottom-windowHeight){ render(getElements(),true); } },100); } function onResize(){//窗口缩放时重新排列 if(calculateColumns()==waterfall.column_num) return; //列数未改变,不需要重排 var $cells=waterfall.$waterfall.find(setting.cell_selector); waterfall.$columns.remove(); waterfall.$columns=creatColumn(); render($cells,false); //重排已有元素时强制不渐显 } })(jQuery);

The above is the editor’s introduction to the combination of PHP+Jquery and ajax to achieve the drop-down and fade-out waterfall flow effect [no plug-ins required] Introduction, I hope it will be helpful to you. If you have any questions during use, please leave me a message and the editor will reply to you in time. At the same time, we are also very grateful for everyone's support for the PHP Chinese website!

For more PHP+Jquery combined with ajax to achieve drop-down and fade-out waterfall flow effect [no plug-ins required] please pay attention to the PHP Chinese website for related articles!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:PHP strip_tags method to retain multiple HTML tagsNext article:PHP strip_tags method to retain multiple HTML tags

Related articles

See more