content-paging.js
3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/**
* @description 根据水平线分页
* @author ly yue.liu@yoho.cn
* @since 2014-12-17
*/
var $=require("jquery");
var slide = require("./photoslide");
var flip = require("./flip");
(function(global, undefined) {
var ContentPaging = function(element, options) {
this.options = options;
this.$element = $(element);
this.$contentWrap = this.$element.find(this.options.pageContentWrap);
this.$coverPic = this.$element.find(this.options.coverPic);
this.dataArr = this.$contentWrap.html().split("<hr>");
this.len = this.dataArr.length;
this.init();
};
ContentPaging.DEFAULTS = {
coverPic:".pic-body",
pageContentWrap:".text-body"
};
ContentPaging.prototype = {
constructor: ContentPaging,
init:function(){
//this.$contentWrap.text(this.$contentWrap.html());
this._browserjuge();
this._creat();
this._bindEvent();
this.pagination(0);
},
//兼容ie8
_browserjuge:function(){
//检测ie版本,ie8采用1024分辨率
var browser=navigator.appName;
var b_version=navigator.appVersion;
var version=b_version.split(";");
var trim_Version=version[1].replace(/[ ]/g,"");
if(browser=="Microsoft Internet Explorer" && trim_Version=="MSIE8.0" || browser=="Microsoft Internet Explorer" && trim_Version=="MSIE7.0"){
this.dataArr = this.$contentWrap.html().split("<HR>");
this.len = this.dataArr.length;
}
},
// 主函数,主要是创建节点,绑定事件。
_creat: function(){
// 创建分页按钮
this._creatPagination();
},
_creatPagination: function(){
if(this.len<=1) return;
var $paginationWrap = $('<div class="pager-content detail-pagination"><div class="channel-index-pager"></div></div>');
var $paginationItem;
this.$contentWrap.after($paginationWrap);
for(var i=0;i<this.len;i++){
$paginationItem = $('<a class="pager-item" href="javascript:;">'+(i+1)+'</a>');
$(".detail-pagination .channel-index-pager").append($paginationItem);
}
},
/*
* 分页函数
*/
pagination: function(index) {
if(this.len<=1) return;
//除了第一页,均不显示封面图
if(index == 0){
this.$coverPic.show();
}else{
this.$coverPic.hide();
}
this.$contentWrap.html(this.dataArr[index]);
$(".detail-pagination .channel-index-pager").find("a").removeClass("pager-item-choosen").eq(index).addClass("pager-item-choosen");
$("body").animate({scrollTop:0},10);
$(document.documentElement).animate({scrollTop:0},10);
},
/*
* 插件事件绑定
*/
_bindEvent: function(){
var that = this;
$(".detail-pagination .channel-index-pager").on("click","a",function(){
var index = $(this).index();
that.pagination(index);
});
},
/**跳转到某一个分页**/
jumpTo: function(index){
pagination(index);
}
};
function Plugin(option, _relatedTarget) {
return this.each(function() {
var $this = $(this);
var data = $this.data('yoho.ContentPaging');
var options = $.extend({}, ContentPaging.DEFAULTS, $this.data(), typeof option == 'object' && option);
// 如果没有初始化,则初始化该对象,并且把该对象绑定到此元素的yoho.PicSilder属性下。
if (!data) $this.data('yoho.ContentPaging', (data = new ContentPaging(this, options)));
// 如果传递的options是一个字符串,则表示调用改对象的原型方法。
if (typeof option == 'string') data[option](_relatedTarget);
});
};
$.fn.contentPaging = Plugin;
$.fn.contentPaging.Constructor = ContentPaging;
})(this);