Authored by weiqingting

男装添加滚动无限功能

@@ -2,17 +2,7 @@ @@ -2,17 +2,7 @@
2 <div class="commodity"> 2 <div class="commodity">
3 3
4 <ul class="clearfix"> 4 <ul class="clearfix">
5 - {{#each commodity}}  
6 - <li>  
7 - {{# state}}  
8 - <i class="commodity-tag{{.}}"></i>  
9 - {{/ state}}  
10 - <a href="{{url}}"><div class="commodity-img"><img class="lazy" data-original="{{img}}"/></div>  
11 - <p class="commodity-name">{{name}}</p>  
12 - <p class="commodity-price"><span>¥{{price}}</span></p>  
13 - </a>  
14 - </li>  
15 - {{/each}} 5 +
16 </ul> 6 </ul>
17 <div class="loading"> 7 <div class="loading">
18 <p>Loading...</p> 8 <p>Loading...</p>
  1 +var $ = require('jquery');
  2 +
  3 +
  4 +function infiniteLoad(options){
  5 + var defaults={
  6 + index:0,
  7 + isload:true,//是否正在加载
  8 + isrun:true,//判断是否执行
  9 + offset:{
  10 + height:new Function(),
  11 + width:new Function()
  12 + }
  13 + };
  14 + this.registerEvent = {
  15 + before: [],
  16 + change: [],
  17 + after: []
  18 + };
  19 + this.options = $.extend(true, {}, defaults, options);
  20 + return this;
  21 +};
  22 + infiniteLoad.prototype.on = function (name, callback) {
  23 + var g = this;
  24 + var _e = g.registerEvent[name];
  25 + if (_e) {
  26 + _e.push(callback);
  27 + }
  28 + return _e;
  29 +};
  30 +infiniteLoad.prototype.off = function (name, callback) {
  31 + var g = this;
  32 + var _e = g.registerEvent[name];
  33 + var e = [];
  34 + $.each(_e, function (name, _callback) {
  35 + if (_callback === callback) {
  36 + e.push(name);
  37 + }
  38 + });
  39 + $.each(e.reverse(), function (name, _callback) {
  40 + _e.splice(_callback, 1);
  41 + });
  42 +};
  43 +infiniteLoad.prototype.exect=function(key,params){
  44 + var g=this,p=this.options;
  45 + if (g.registerEvent[key]&&g.registerEvent[key].length > 0) {
  46 + for (_e in g.registerEvent[key]) {
  47 + g.registerEvent[key][_e](params);
  48 + }
  49 + }
  50 +};
  51 +infiniteLoad.prototype.init = function() {
  52 + var g=this,p=this.options;
  53 + function __loadMore(){
  54 + if(p.isload&&g.__directionCalculation()){
  55 + p.isload=false;
  56 + p.index++;
  57 + g.exect("after",p);
  58 + }
  59 + g.exect("change",p);
  60 + }
  61 + g.exect("before",p);
  62 + $(window).scroll(__loadMore);
  63 +};
  64 +infiniteLoad.prototype.emit=function(){
  65 + var g=this,p=this.options;
  66 + p.isload=true;
  67 +};
  68 +infiniteLoad.prototype.__directionCalculation=function(){
  69 + var g=this,p=this.options;
  70 + if(p.offset.height()>0&&$(window).scrollTop() + $(window).height() >= p.offset.height()){
  71 + return true;
  72 + }
  73 + if(p.offset.width()>0&&$(window).scrollLeft() + $(window).width() >= p.offset.width()){
  74 + return true;
  75 + }
  76 + return false;
  77 +}
  78 +module.exports =infiniteLoad;
  1 +var $ = require('jquery');
  2 +var Lazyload = require('yoho.lazyload');
  3 +var Handlebars=require('yoho.handlebars');
  4 +var infiniteLoad=require('../common/infiniteLoad');
  5 +(function($) {
  6 + var $container=$("div.commodity ul");
  7 + var $load=$(".loading");
  8 + var load=new infiniteLoad({
  9 + offset:{
  10 + height:function(){return parseFloat($container.offset().top)+parseFloat($container.height())}
  11 + }
  12 + });
  13 + load.on("after",function(p){
  14 + var options={
  15 + type: 'POST',
  16 + url: '/boys/commodity',
  17 + data: {
  18 + pageIndex: p.index,
  19 + pageCount:10,
  20 + flag:'boy'
  21 + },
  22 + success: function(data) {
  23 + var code = data.code;
  24 +
  25 + if (code === 200) {
  26 + var myTemplate = Handlebars.compile(load.tpl);
  27 + $container.append(myTemplate(data.commodity));
  28 + //懒加载插件貌似有点问题,图片先直接展示
  29 + //Lazyload($container.find('img.lazy'));
  30 + load.emit();
  31 + }else{
  32 + $load.html('END');
  33 + }
  34 + },
  35 + error: function() {
  36 + $load.html('网络断开连接了~');
  37 + }
  38 + };
  39 + $.ajax(options);
  40 + //请求数据
  41 +
  42 +
  43 + });
  44 + load.on("before",function(){
  45 + //请求模板
  46 + load.tpl='{{#each this}}'
  47 + +'<li>'
  48 + +' {{# state}}'
  49 + +' <i class="commodity-tag{{.}}"></i>'
  50 + +' {{/ state}}'
  51 + +' <a href="{{url}}"><div class="commodity-img"><img class="lazy" data-original="{{img}}" src="{{img}}"/></div>'
  52 + +' <p class="commodity-name">{{name}}</p>'
  53 + +' <p class="commodity-price"><span>¥{{price}}</span></p>'
  54 + +' </a>'
  55 + +'</li>'
  56 + +'{{/each}}';
  57 +
  58 + load.emit();
  59 + })
  60 + load.init();
  61 +}($));
@@ -8,6 +8,11 @@ var $ = require('jquery'); @@ -8,6 +8,11 @@ var $ = require('jquery');
8 8
9 require('../common/linkage-slider'); 9 require('../common/linkage-slider');
10 require('../common/slider2'); 10 require('../common/slider2');
  11 +require('../common/newArrivls');
  12 +
11 13
12 $('.slide-container').linkageSlider(); 14 $('.slide-container').linkageSlider();
13 $('.img-brand').slider2(); 15 $('.img-brand').slider2();
  16 +
  17 +
  18 +
@@ -13,7 +13,9 @@ @@ -13,7 +13,9 @@
13 "spm": { 13 "spm": {
14 "main": "index.js", 14 "main": "index.js",
15 "dependencies": { 15 "dependencies": {
16 - "jquery": "1.8.3" 16 + "jquery": "1.8.3",
  17 + "yoho.lazyload": "1.0.0",
  18 + "yoho.handlebars": "3.0.3"
17 }, 19 },
18 "devDependencies": { 20 "devDependencies": {
19 "expect.js": "0.3.1" 21 "expect.js": "0.3.1"
@@ -274,8 +274,10 @@ @@ -274,8 +274,10 @@
274 } 274 }
275 } 275 }
276 .tpl-category{ 276 .tpl-category{
  277 + height: 228px;
277 padding: 10px 0; 278 padding: 10px 0;
278 background-color: #f8f8f8; 279 background-color: #f8f8f8;
  280 + overflow: hidden;
279 a{ 281 a{
280 float: left; 282 float: left;
281 width: 50%; 283 width: 50%;
@@ -235,7 +235,32 @@ class BoysController extends AbstractAction @@ -235,7 +235,32 @@ class BoysController extends AbstractAction
235 'href' => '', 235 'href' => '',
236 'name' => 'MORE' 236 'name' => 'MORE'
237 ) 237 )
238 - ), 238 + )
  239 + )
  240 + )
  241 + );
  242 + $this->_view->display('index', $data);
  243 + }
  244 + /**
  245 + * 男装首页 新品上架 接口数据
  246 + *
  247 + * @param int $pageIndex 当前页数
  248 + * @param int $pageCount 一页显示个数
  249 + * @param string flag 类型(男装/女装等,用于区分)
  250 + * @return json
  251 + */
  252 + public function commodityAction()
  253 + {
  254 + $result = array();
  255 +
  256 + do {
  257 + /* 判断是不是AJAX请求 */
  258 + if (!$this->isAjax()) {
  259 + break;
  260 + }
  261 +
  262 + $result=array(
  263 + 'code'=>200,
239 'commodity'=>array( 264 'commodity'=>array(
240 array( 265 array(
241 'url' =>'http://www.muji.com.cn/cn/store/goods/4547315967308' , 266 'url' =>'http://www.muji.com.cn/cn/store/goods/4547315967308' ,
@@ -271,25 +296,11 @@ class BoysController extends AbstractAction @@ -271,25 +296,11 @@ class BoysController extends AbstractAction
271 'name'=> '优显led触控台灯', 296 'name'=> '优显led触控台灯',
272 'price'=> 168, 297 'price'=> 168,
273 'state'=> 1 //状态 298 'state'=> 1 //状态
274 - ),  
275 - array(  
276 - 'url' =>'http://www.muji.com.cn/cn/store/goods/4547315967308' ,  
277 - 'img'=> 'http://img11.static.yhbimg.com/yhb-img01/2015/12/01/02/01c21e6610eefdc5ebd7ad890e49b09c2d.jpg',  
278 - 'name'=> '优显led触控台灯',  
279 - 'price'=> 168,  
280 - 'state'=> 1 //状态  
281 - ),  
282 - array(  
283 - 'url' =>'http://www.muji.com.cn/cn/store/goods/4547315967308' ,  
284 - 'img'=> 'http://img11.static.yhbimg.com/yhb-img01/2015/12/01/02/01c21e6610eefdc5ebd7ad890e49b09c2d.jpg',  
285 - 'name'=> '优显led触控台灯',  
286 - 'price'=> 168,  
287 - 'state'=> 1 //状态  
288 - )  
289 - )  
290 ) 299 )
291 ) 300 )
292 ); 301 );
293 - $this->_view->display('index', $data); 302 + } while (false);
  303 +
  304 + $this->echoJson($result);
294 } 305 }
295 } 306 }