首页 / CSS3 / CSS3实现整屏切换效果
CSS3实现整屏切换效果
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了CSS3实现整屏切换效果,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6171字,纯文字阅读大概需要9分钟。
内容图文
总是能看见非常多广告或者站点都是使用整屏滚动的效果,一直看着都心痒痒,想自己也实现一个。近期刚学习到css3的动画效果,所以尝试使用css3做了一个整屏切换。
页面结构
实现思路与大众方法相似。如图
每一个section就是一页内容。它的大小充满了屏幕(红色区域)。一个container由多个section构成,我们通过改变container的位置,来达到页面切换的效果。
container向下走。页面好像上移了,container向上走,页面就下移了。
html结构例如以下:
<!DOCTYPE html>
<html>
<headlang="ch">
<metacharset="UTF-8">
<!--适配移动端-->
<metaname=”viewport”content="width=device-width, user-scalable=no, initial-scale=1.0">
<title>
</title>
<style>
body, html{
padding:0;
margin:0;
}body, html{
height:100%;
/**隐藏滚动栏**/overflow: hidden;
}#container, .section{
height:100%;
}#section0{
background-color:#83af9b;
}#section1{
background-color:#764d39;
}#section2{
background-color:#ff9000;
}#section3{
background-color:#380d31;
}
</style>
</head>
<body>
<divid="container">
<divclass="section"id="section0">
</div>
<divclass="section"id="section1">
</div>
<divclass="section"id="section2">
</div>
<divclass="section"id="section3">
</div>
</div>
</body>
</html>
事件监听
此时窗口里仅仅显示一个页面,我们给其加上滚动监听。由于firefox和非firefox浏览器对滚动监听支持不同。firefox浏览器向上滚动是-120,向下滚动是120,而其它浏览器向上是5,向下是-5,所以须要作推断:
<scriptsrc="http://code.jquery.com/jquery-latest.js">
</script>
<script>
//当前页面索引
var curIndex = 0;
var scrollFunc = function(e) {
e = e || window.event;
var t = 0;
if (e.wheelDelta) {//IE/Opera/Chrome
t = e.wheelDelta;
if (t > 0 && curIndex > 0) {
//上滚动
movePrev();
} elseif (t < 0 && curIndex < sumCount - 1) {
//下滚动
moveNext();
}
} elseif (e.detail) {//Firefox
t = e.detail;
if (t < 0 && curIndex > 0) {
//上滚动
movePrev();
} elseif (t > 0 && curIndex < sumCount - 1) {
//下滚动
moveNext();
}
}
};
functionmoveNext(){
}
functionmovePrev(){
}
functioninit(){/*注冊事件*/if (document.addEventListener) {
document.addEventListener(‘DOMMouseScroll‘, scrollFunc, false);
}//W3C
window.onmousewheel = document.onmousewheel = scrollFunc;//IE/Opera/Chrome
}
init();
</script>
为了防止在第一个页面用户上滚。最后一个页面用户下滚。所以用curIndex代表当前页面索引在滚动时作了监听。当然假设要使页面循环滚动。仅仅需改动条件限制就可以。
增加动画
动画使用到了css3里的transform属性的translate3D,我们首先须要获取到屏幕的高度,然后当页面切换的时候将container上移一个屏幕高度或下移一个屏幕高度。
使用translate3D的原因是在手机端会开启硬件加速,使动画更流畅。它接收三个參数,各自是x轴、y轴和z轴的位移。如
transform: tanslate3D(10px, 30px, 0);
改动后的js代码例如以下:
<script>
//当前页面索引
var curIndex = 0;
//container元素var container = $("#container");
//页面总数var sumCount = $(".section").length;
//窗口元素var $window = $(window);
//动画时间var duration = 500;
var scrollFunc = function(e) {
e = e || window.event;
var t = 0;
if (e.wheelDelta) {//IE/Opera/Chrome
t = e.wheelDelta;
if (t > 0 && curIndex > 0) {
//上滚动
movePrev();
} elseif (t < 0 && curIndex < sumCount - 1) {
//下滚动
moveNext();
}
} elseif (e.detail) {//Firefox
t = e.detail;
if (t < 0 && curIndex > 0) {
//上滚动
movePrev();
} elseif (t > 0 && curIndex < sumCount - 1) {
//下滚动
moveNext();
}
}
};
functionmoveNext(){
container.css("transform", "translate3D(0, -" + (++curIndex) * $window.height() + "px, 0)");
}
functionmovePrev(){
container.css("transform", "translate3D(0, -" + (--curIndex) * $window.height() + "px, 0)");
}
functioninit(){/*注冊事件*/if (document.addEventListener) {
document.addEventListener(‘DOMMouseScroll‘, scrollFunc, false);
}//W3C
window.onmousewheel = document.onmousewheel = scrollFunc;//IE/Opera/Chrome//设置动画
container.css({
"transition": "all 0.5s",
"-moz-transition": "all 0.5s",
"-webkit-transition": "all 0.5s"
});
}
</script>
为了防止页面在滚动的时候用户继续滚动打乱节奏,能够用时间来强制控制,即在滚动期间不同意调用moveNext和movePrev函数,终于代码例如以下:
<!DOCTYPE html>
<html>
<headlang="ch">
<metacharset="UTF-8">
<metaname=”viewport”content="width=device-width, user-scalable=no, initial-scale=1.0">
<title>
</title>
<style>
body, html{
padding:0;
margin:0;
}body, html{
height:100%;
overflow: hidden;
}#container, .section{
height:100%;
}.section{
background-color:#000;
background-size: cover;
background-position:50% 50%;
}#section0{
background-color:#83af9b;
}#section1{
background-color:#764d39;
}#section2{
background-color:#ff9000;
}#section3{
background-color:#380d31;
}
</style>
</head>
<body>
<divid="container">
<divclass="section"id="section0">
</div>
<divclass="section"id="section1">
</div>
<divclass="section"id="section2">
</div>
<divclass="section"id="section3">
</div>
</div>
<scriptsrc="http://code.jquery.com/jquery-latest.js">
</script>
<script>
var curIndex = 0;
var container = $("#container");
var sumCount = $(".section").length;
var $window = $(window);
var duration = 500;
//时间控制var aniTime = 0;
var scrollFunc = function(e) {//假设动画还没运行完,则returnif(newDate().getTime() < aniTime + duration){
return;
}
e = e || window.event;
var t = 0;
if (e.wheelDelta) {//IE/Opera/Chrome
t = e.wheelDelta;
if (t > 0 && curIndex > 0) {
//上滚动
movePrev();
} elseif (t < 0 && curIndex < sumCount - 1) {
//下滚动
moveNext();
}
} elseif (e.detail) {//Firefox
t = e.detail;
if (t < 0 && curIndex > 0) {
//上滚动
movePrev();
} elseif (t > 0 && curIndex < sumCount - 1) {
//下滚动
moveNext();
}
}
};
functionmoveNext(){//获取动画開始时的时间
aniTime = newDate().getTime();
container.css("transform", "translate3D(0, -" + (++curIndex) * $window.height() + "px, 0)");
}
functionmovePrev(){//获取动画開始时的时间
aniTime = newDate().getTime();
container.css("transform", "translate3D(0, -" + (--curIndex) * $window.height() + "px, 0)");
}
functioninit(){/*注冊事件*/if (document.addEventListener) {
document.addEventListener(‘DOMMouseScroll‘, scrollFunc, false);
}//W3C
window.onmousewheel = document.onmousewheel = scrollFunc;//IE/Opera/Chrome
container.css({
"transition": "all 0.5s",
"-moz-transition": "all 0.5s",
"-webkit-transition": "all 0.5s"
});
}
init();
</script>
</body>
</html>
原文:http://www.cnblogs.com/ljbguanli/p/7199157.html
内容总结
以上是互联网集市为您收集整理的CSS3实现整屏切换效果全部内容,希望文章能够帮你解决CSS3实现整屏切换效果所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。