contentPaging.js 3.58 KB
/**
 * @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);