Authored by 陈轩

simple sticky

@@ -23,7 +23,7 @@ @@ -23,7 +23,7 @@
23 </div> 23 </div>
24 {{!--shop-tab-nav: 红人首页 | 全部商品--}} 24 {{!--shop-tab-nav: 红人首页 | 全部商品--}}
25 <div> 25 <div>
26 - <nav class="shop-tabs"> 26 + <nav class="shop-tabs" data-sticky>
27 <a href="#" class="shop-tab-nav active">红人首页</a> 27 <a href="#" class="shop-tab-nav active">红人首页</a>
28 <em>|</em> 28 <em>|</em>
29 <a href="#" class="shop-tab-nav">全部商品</a> 29 <a href="#" class="shop-tab-nav">全部商品</a>
@@ -24,8 +24,8 @@ module.exports = { @@ -24,8 +24,8 @@ module.exports = {
24 // imSocket: 'ws://im.yohobuy.com:10240', 24 // imSocket: 'ws://im.yohobuy.com:10240',
25 // imCs: 'http://im.yohobuy.com/api', 25 // imCs: 'http://im.yohobuy.com/api',
26 // imServer: 'http://im.yohobuy.com/server' 26 // imServer: 'http://im.yohobuy.com/server'
27 - api: 'http://192.168.102.205:8080/gateway/',  
28 - // api: 'http://api.yoho.cn/', 27 + // api: 'http://192.168.102.205:8080/gateway/',
  28 + api: 'http://api.yoho.cn/',
29 service: 'http://service.yoho.cn/', 29 service: 'http://service.yoho.cn/',
30 liveApi: 'http://api.live.yoho.cn/', 30 liveApi: 'http://api.live.yoho.cn/',
31 singleApi: 'http://single.yoho.cn/', 31 singleApi: 'http://single.yoho.cn/',
@@ -6,15 +6,20 @@ @@ -6,15 +6,20 @@
6 6
7 'use strict'; 7 'use strict';
8 8
  9 +const dataAPI = '[data-sticky]';
  10 +
9 var Sticky = function(elem, options) { 11 var Sticky = function(elem, options) {
10 this.options = $.extend({}, Sticky.DEAFAULT, options); 12 this.options = $.extend({}, Sticky.DEAFAULT, options);
11 this.$elem = $(elem); 13 this.$elem = $(elem);
12 this.$holder = $('<div></div>').height(this.$elem.height()); 14 this.$holder = $('<div></div>').height(this.$elem.height());
  15 + this.$holder = this.$elem.wrap(this.$holder).parent();
  16 +
  17 + this.stickyState = null;
  18 +
  19 +
13 this.$target = $(this.options.target) 20 this.$target = $(this.options.target)
14 .on('scroll.yoho.sticky', $.proxy(this.checkPosition, this)); 21 .on('scroll.yoho.sticky', $.proxy(this.checkPosition, this));
15 22
16 - this.$elem.wrapper(this.$holder);  
17 -  
18 this.checkPosition(); 23 this.checkPosition();
19 }; 24 };
20 25
@@ -24,11 +29,47 @@ Sticky.DEAFAULT = { @@ -24,11 +29,47 @@ Sticky.DEAFAULT = {
24 }; 29 };
25 30
26 Sticky.prototype.checkPosition = function() { 31 Sticky.prototype.checkPosition = function() {
27 - if (!this.$elem.is('::visible')) { 32 + let options = this.options;
  33 +
  34 + if (!this.$elem.is(':visible')) {
28 return; 35 return;
29 } 36 }
  37 +
  38 + let offsetTop = this.$holder.offset().top - options.top;
  39 + let scrollTop = this.$target.scrollTop();
  40 +
  41 + let willSticky = scrollTop >= offsetTop;
  42 +
  43 + this.$elem.toggleClass('sticky', willSticky);
30 }; 44 };
31 45
32 46
  47 +function Plugin(option) {
  48 + this.each(function() {
  49 + let $this = $(this);
  50 + let data = $this.data('yoho.sticky');
  51 + let options = typeof option === 'object' && option;
  52 +
  53 + if (!data) {
  54 + $this.data('yoho.sticky', new Sticky($this, options));
  55 + }
  56 +
  57 + if (typeof option === 'string') {
  58 + $this[option]();
  59 + }
  60 + });
  61 +}
  62 +
  63 +$.fn.Stricky = Plugin;
  64 +$.fn.Stricky.Constructor = Sticky;
  65 +
  66 +window.onload = function() {
  67 + $(dataAPI).each(function() {
  68 + let $this = $(this);
  69 +
  70 + Plugin.call($this);
  71 + });
  72 +};
  73 +
33 74
34 module.exports = Sticky; 75 module.exports = Sticky;
1 require('product/shop/redshop.page.css'); 1 require('product/shop/redshop.page.css');
2 2
  3 +require('plugin/sticky');
3 require('./shop/coupon'); 4 require('./shop/coupon');
  1 +.sticky {
  2 + position: fixed;
  3 + top: 0;
  4 +}
1 .shop-tabs { 1 .shop-tabs {
2 display: flex; 2 display: flex;
3 justify-content: space-around; 3 justify-content: space-around;
  4 + width: 100%;
4 font-size: 30px; 5 font-size: 30px;
5 line-height: 80px; 6 line-height: 80px;
6 border-top: 1PX solid #dededf; 7 border-top: 1PX solid #dededf;
7 border-bottom: 1PX solid #dededf; 8 border-bottom: 1PX solid #dededf;
  9 + background-color: #fff;
8 10
9 em { 11 em {
10 color: #dededf; 12 color: #dededf;
11 } 13 }
  14 +
  15 + &.sticky {
  16 + z-index: 10;
  17 + }
12 } 18 }
13 19
14 .shop-tab-nav { 20 .shop-tab-nav {
1 @charset "utf-8"; 1 @charset "utf-8";
2 2
  3 +@import "../../layout/sticky";
3 @import "shop-index-coupon"; 4 @import "shop-index-coupon";
4 @import "reds-shop/header"; 5 @import "reds-shop/header";
5 @import "reds-shop/nav-tab"; 6 @import "reds-shop/nav-tab";