哪位大佬能幫我看一下,只能滑到第二頁怎么解決
HTML代碼:
?<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>全屏切換效果</title>
<style type="text/css">
? *{
padding: 0;
margin: 0;
/*overflow:hidden;*/
}
html,body{
height: 100%;/*//全屏要素:1全屏的元素及其父元素都要設置*/
? ? ? ? ? ? ?/*//height為100%,HTML和body標簽設置height為100%*/
overflow:hidden;
}
#container,.sections,.section{
height: 100%;
/*position: relative;*/
}
#section0,
#section1,
#section2,
#section3{
background-color: #000;
background-size: cover;
background-position: 50% 50%;
text-align:center;/*//使背景圖始終位于屏幕的中心*/
color:white;
}
#section0{
background-image: url(images/1.jpg);
}
#section1{
background-image: url(images/2.jpg);
}
#section2{
background-image: url(images/3.jpg);
}
#section3{
background-image: url(images/4.jpg);
}
/*.left{
float:left;
width:25%;
}*/
</style>
</head>
<body>
<div id="container" ?data-PageSwitch>
<div class="sections">
<div class="section ?active" id="section0">
<div class="intro">
<h1 class="title">switchPage</h1>
<p>Create Beautiful Fullscreen Scrolling Websites</p>
</div>
</div>
<div class="section " id="section1">
<div class="intro">
<h1 class="title">Example</h1>
<p>HTML markup example to define 4 sections</p>
</div>
</div>
<div class="section " id="section2">
<div class="intro">
<h1 class="title">Example</h1>
<p>The plug-in configuration parameters</p>
</div>
</div>
<div class="section " id="section3">
<div class="intro">
<h1 class="title">THE END</h1>
<p>Everything will be okay in the end. If it's not okay, it's not the end.</p>
</div>
</div>
</div>
</div>
<!-- <script type="text/javascript" src="https://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
<script type="text/javascript" src="pageswitch.js"></script> -->
<script type="text/javascript" src="jquery-1.11.2.min.js"></script>
<script type="text/javascript" src="js/indexjs.js"></script>
</body>
</html>?
js代碼:
(function($){
/*說明:獲取瀏覽器前綴*/
/*實現:判斷某個元素的css樣式中是否存在transition屬性*/
/*參數:dom元素*/
/*返回值:boolean,有則返回瀏覽器樣式前綴,否則返回false*/
var _prefix=(function(temp){
var aPrefix = ["webkit", "Moz", "o", "ms"],
props = "";
for(var i in aPrefix){
props = aPrefix[i] + "Transition";
if(temp.style[ props ] !== undefined){
return "-"+aPrefix[i].toLowerCase()+"-";
}
}
return false;
})(document.createElement(pageswitch));
//定義pageswitch對象
var pageswitch = (function(){
function pageswitch(element, options){
this.settings = $.extend(true, $.fn.pageswitch.defaults, options||{});
this.element = element;
this.init();
}
pageswitch.prototype={
/*說明:初始化插件*/
/*實現:初始化dom結構,布局,分頁及綁定事件*/
init : function(){
var me = this;
me.selectors = me.settings.selectors;
me.sections = me.element.find(me.selectors.sections);
me.section = me.sections.find(me.selectors.section);
me.direction = me.settings.direction == "vertical" ? true : false;
me.pagesCount = me.pagesCount();
me.index = (me.settings.index >= 0 && me.settings.index < pagesCount) ? me.settings.index : 0;
me.canScroll = true;
if(!me.direction){
me._initLayout();
}
if(me.settings.pagination){
me._initPaging();
}
me._initEvent();
},
/*說明:獲取滑動頁面數量*/
pagesCount : function(){
return this.section.length;
},
/*說明:獲取滑動的寬度(橫屏滑動)或高度(豎屏滑動)*/
switchLength : function(){
return this.direction ? this.element.height() : this.element.width();
},
/*說明:向前滑動即上一頁*/
prve : function(){
var me = this;
if(me.index > 0){
me.index--;
}else if(me.settings.loop){
me.index = me.pagesCount - 1;
}
me._scrollPage();
},
/*說明:向后滑動即下一頁*/
next : function(){
var me = this;
if(me.index < me.pagesCount){
me.index ++;
}else if(me.settings.loop){
me.index = 0;
}
me._scrollPage();
},
/*說明:主要針對橫屏情況進行頁面布局*/
_initLayout : function(){
// var me = this;
// var width=(me.pagesCount*100)+"%";
? ? // ? ? ? ? ? ? var cellWidth=(100/me.pagesCount).toFixed(2)+"%";//保持兩位小數toFixed(2)
? ? // ? ? ? ? ? ? me.sections.width(width);
? ? // ? ? ? ? ? ? me.section.width(cellWidth).css("float", "left");
? ? ? ? ? ? ? ? var me = this;
if(!me.direction){
var width = (me.pagesCount * 100) + "%",
cellWidth = (100 / me.pagesCount).toFixed(2) + "%";
me.sections.width(width);
me.section.width(cellWidth).css("float", "left");
}
if(me.index){
me._scrollPage(true);
}
},
? ? ? ? ? ? /*說明:實現分頁的dom結構及其css樣式*/
_initPaging : function(){
var me = this,
pagesClass = me.selectors.page.substring(1);//截取字符串從1開始到結尾
me.activeClass = me.selectors.active.substring(1);
var pageHtml = "<ul class="+pagesClass+">";
for(var i = 0 ; i < me.pagesCount; i++){
pageHtml += "<li></li>";
}
pageHtml+="</ul>";
me.element.append(pageHtml);//動態插入分頁結構
var pages = me.element.find(me.selectors.page);
me.pageItem = pages.find("li");
me.pageItem.eq(me.index).addClass(me.activeClass);
if(me.direction){
pages.addClass("vertical");
}else{
pages.addClass("horizontal");
}
},
/*說明:初始化插件事件*/
_initEvent : function(){
var me = this;
/*綁定分頁click事件*/
me.element.on("click", me.selectors.pages + " li", function(){
me.index = $(this).index();
me._scrollPage();
});
/*綁定鼠標滾輪事件*/
me.element.on("mousewheel DOMMouseScroll", function(e){
if(me.canScroll){
var delta = e.originalEvent.wheelDelta || -e.originalEvent.detail;
if(delta > 0 && (me.index && !me.settings.loop || me.settings.loop)){
me.prve();
}else if(delta < 0 && (me.index < (me.pagesCount-1) && !me.settings.loop ||me.settings.loop)){
me.next();
}
?}
? ?});
? ? ? ? ? ? ? ? //綁定鍵盤事件
if(me.settings.keyboard){
$(window).on("keydown",function(e){
var keyCode = e.keyCode;
if(keyCode == 37 || keyCode == 38){//左鍵上鍵
me.prve();
}else if(keyCode == 39 || keyCode == 40){//右鍵下鍵
me.next();
}
});
}
/*綁定窗口改變事件*/
/*為了不頻繁調用resize的回調方法,做了延遲*/
$(window).resize(function(){
var currentLength=me.switchLength();//獲取當前頁面的高度
var offset = me.settings.direction ? me.section.eq(me.index).offset().top :?
? me.section.eq(me.index).offset().left;//當前頁面距離文檔的坐標值
if(Math.abs(offset) > currentLength/2 && me.index < (me.pagesCount - 1)){
me.index++;
}
if(me.index){
me._scrollPage();
}
});
/*支持CSS3動畫的瀏覽器,綁定transitionend事件(即在動畫結束后調用起回調函數)*/
// if(_prefix){
me.sections.on("transitionend webkitTransitionEnd oTransitionEnd otransitionend", function(){
me.canScroll = true;
if(me.settings.callback && $.type(me.settings.callback) == "function"){
me.settings.callback();
}
});
},
/*滑動動畫*/
_scrollPage : function(){
var me = this;
var dest = me.section.eq(me.index).position();
if(!dest) return;
me.canScroll = false;
if(_prefix){
var translate = me.direction ? "translateY(-"+dest.top+"px)" : "translateX(-"+dest.left+"px)";
me.sections.css(_prefix+"transition", "all" + me.settings.duration + "ms " + me.settings.easing);
me.sections.css(_prefix+"transform" , translate);
}else{
var animateCss = me.direction ? {top : -dest.top} : {left : -dest.left};
me.sections.animate(animateCss, me.settings.duration, function(){
me.canScroll = true;
if(me.settings.callback && $.type(me.settings.callback) == "function"){
me.settings.callback();
}
});
}
if(me.settings.pagination){
me.pageItem.eq(me.index).addClass(me.activeClass).siblings("li").removeClass(me.activeClass);
}
},
};
return pageswitch;
? ? })();
? ? //單例模式
$.fn.pageswitch=function(options){//傳入參數
return this.each(function(){//
var me=$(this),
? ? instance=me.data("pageswitch");//存放插件實例
? ? if(!instance){//判斷實例是為為空,空則創一個實例存放在me.data
instance=new pageswitch(me,options);
me.data("pageswitch",instance);
}
if($.type(options)==="string") return instance[options]();
// $("div").pageswitch("init");//這樣就可以調用init方法
? ? ? });
};
$.fn.pageswitch.defaults = {
selectors : {
sections : ".sections",
section : ".section",
page : ".pages",
active : ".active",
},
index : 0, //頁面開始的索引
easing : "ease", //動畫效果
duration : 500, //動畫執行時間
loop : false, //是否循環切換
pagination : true, //是否進行分頁
keyboard : true, //是否觸發鍵盤事件
direction : "vertical", //滑動方向vertical豎屏,horizontal橫向
callback : "" //回調函數
};
$(function(){
$("[data-PageSwitch]").pageswitch();//配置插件
});
})(jQuery);