HTML防止手机多屏浏览,js多屏切换效果(兼容IE以及移动端)

需要引入jQuery以及iscroll.js

外层容器.ctn的height取决于有多少个滚动元素.box 高度为.box的个数*100% 例如有4个.box 则高度为 4*100%

滚动元素因为是满屏切换,所以高度为总容器的 n分之一 。如有四个.box元素。则每个的高度为 四分之一 即25%

JS部分:

if (window.addEventListener) {

var newScroll = new IScroll("#container", {

mouseWheel: true,

momentum: false,

snap: true

})

} else {

// IE7-IE8

var newScroll = {

content: $("#container > div"),

currentPage: {

pageY: 0

},

next: function() {

this.currentPage.pageY++;

var index = this.currentPage.pageY;

if (index > 9) {

index = 9;

return this;

}

this.content.animate({

top: -100 * index + "%"

}, this.scrollEnd);

},

prev: function() {

this.currentPage.pageY--;

var index = this.currentPage.pageY;

if (index < 0) {

index = 0;

return this;

}

this.content.animate({

top: -100 * index + "%"

}, this.scrollEnd)

},

goToPage: function(x, y) {

this.currentPage.pageY = y;

this.content.animate({

top: -100 * y + "%"

}, this.scrollEnd);

},

on: function(name, fn) {

this[name] = fn;

}

}

}

$(".downbtn").on("click", function() {

newScroll.next()

});

$("#nav li").on("click", function() {

newScroll.goToPage(0, $(this).attr("data-index") * 1)

});

newScroll.on("scrollEnd", function() {

var pageY = newScroll.currentPage.pageY;

$("#nav li").removeClass("navon");

$("#nav li[data-index=" + pageY + "]").addClass("navon");

$(".box").eq(pageY).addClass("active");

});

$(document).bind("keyup", function(event) {

var code = event.keyCode;

if (code == 38) {

newScroll.prev()

} else if (code == 40) {

newScroll.next()

}

});

newScroll.goToPage(0, 0);

CSS部分:

.container {

position:absolute;

left:0;

right:0;

top:0;

bottom:0;

}

.ctn {

height:400%;

position:absolute;

left:0;

right:0;

}

.box {

height:25%;

position:relative;

overflow:hidden;

display:block;

}

.box1 {

background-color:#ccc;

}

.box2 {

background-color:red;

}

.box3 {

background-color:blue;

}

.box4 {

background-color:green;

}

#nav {

position:fixed;

top:100px;

left:40px;

list-style:none;

}

#nav li {

width:20px;

height:20px;

border:1px solid #000;

background-color:#fff;

text-align:center;

cursor:pointer;

margin-bottom:10px;

}

#nav li.navon {

background-color:#000;

border:1px solid #fff;

color:#fff;

}

.downbtn {

display:block;

cursor:pointer;

width:53px;

height:32px;

background:url(icon-down.png) left top no-repeat;

text-indent:-9999px;

overflow:hidden;

position:absolute;

left:50%;

bottom:5%;

margin-left:-26px;

}

HTML部分:

第一屏

下一屏

第二屏

下一屏

第三屏

下一屏

第四屏

下一屏

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值