Html 全屏切换效果
来源 http://www.imooc.com/learn/374
pageswitch.js
(function ($) {
var defaults = {
'container': '#container', //容器
'sections': '.section', //子容器
'easing': 'ease', //特效方式,ease-in,ease-out,linear
'duration': 1000, //每次动画执行的时间
'pagination': true, //是否显示分页
'loop': true, //是否循环
'keyboard': true, //是否支持键盘
'direction': 'vertical', //滑动的方向 horizontal,vertical,
'onpageSwitch': function (pagenum) { }
};
var win = $(window),
container, sections;
var opts = {},
canScroll = true;
var iIndex = 0;
var arrElement = [];
var SP = $.fn.switchPage = function (options) {
opts = $.extend({}, defaults, options || {});
container = $(opts.container),
sections = container.find(opts.sections);
sections.each(function () {
arrElement.push($(this));
});
return this.each(function () {
if (opts.direction == "horizontal") {
initLayout();
}
if (opts.pagination) {
initPagination();
}
if (opts.keyboard) {
keyDown();
}
});
}
//滚轮向上滑动事件
SP.moveSectionUp = function () {
if (iIndex) {
iIndex--;
} else if (opts.loop) {
iIndex = arrElement.length - 1;
}
scrollPage(arrElement[iIndex]);
};
//滚轮向下滑动事件
SP.moveSectionDown = function () {
if (iIndex < (arrElement.length - 1)) {
iIndex++;
} else if (opts.loop) {
iIndex = 0;
}
scrollPage(arrElement[iIndex]);
};
//私有方法
//页面滚动事件
function scrollPage(element) {
var dest = element.position();
if (typeof dest === 'undefined') { return; }
initEffects(dest, element);
}
//重写鼠标滑动事件
$(document).on("mousewheel DOMMouseScroll", MouseWheelHandler);
function MouseWheelHandler(e) {
e.preventDefault();
var value = e.originalEvent.wheelDelta || -e.originalEvent.detail;
var delta = Math.max(-1, Math.min(1, value));
if (canScroll) {
if (delta < 0) {
SP.moveSectionDown();
} else {
SP.moveSectionUp();
}
}
return false;
}
//横向布局初始化
function initLayout() {
var length = sections.length,
width = (length * 100) + "%",
cellWidth = (100 / length).toFixed(2) + "%";
container.width(width).addClass("left");
sections.width(cellWidth).addClass("left");
}
//初始化分页
function initPagination() {
var length = sections.length;
if (length) {
}
var pageHtml = '<ul id="pages"><li class="active"></li>';
for (var i = 1; i < length; i++) {
pageHtml += '<li></li>';
}
pageHtml += '</ul>';
$("body").append(pageHtml);
}
//分页事件
function paginationHandler() {
var pages = $("#pages li");
pages.eq(iIndex).addClass("active").siblings().removeClass("active");
}
//是否支持css的某个属性
function isSuportCss(property) {
var body = $("body")[0];
for (var i = 0; i < property.length; i++) {
if (property[i] in body.style) {
return true;
}
}
return false;
}
//渲染效果
function initEffects(dest, element) {
var transform = ["-webkit-transform", "-ms-transform", "-moz-transform", "transform"],
transition = ["-webkit-transition", "-ms-transition", "-moz-transition", "transition"];
canScroll = false;
if (isSuportCss(transform) && isSuportCss(transition)) {
var traslate = "";
if (opts.direction == "horizontal") {
traslate = "-" + dest.left + "px, 0px, 0px";
} else {
traslate = "0px, -" + dest.top + "px, 0px";
}
container.css({
"transition": "all " + opts.duration + "ms " + opts.easing,
"transform": "translate3d(" + traslate + ")"
});
container.on("webkitTransitionEnd msTransitionend mozTransitionend transitionend", function () {
canScroll = true;
});
} else {
var cssObj = (opts.direction == "horizontal") ? { left: -dest.left} : { top: -dest.top };
container.animate(cssObj, opts.duration, function () {
canScroll = true;
});
}
element.addClass("active").siblings().removeClass("active");
if (opts.pagination) {
paginationHandler();
}
}
//窗口Resize
var resizeId;
win.resize(function () {
clearTimeout(resizeId);
resizeId = setTimeout(function () {
reBuild();
}, 500);
});
function reBuild() {
var currentHeight = win.height(),
currentWidth = win.width();
var element = arrElement[iIndex];
if (opts.direction == "horizontal") {
var offsetLeft = element.offset().left;
if (Math.abs(offsetLeft) > currentWidth / 2 && iIndex < (arrElement.length - 1)) {
iIndex++;
}
} else {
var offsetTop = element.offset().top;
if (Math.abs(offsetTop) > currentHeight / 2 && iIndex < (arrElement.length - 1)) {
iIndex++;
}
}
if (iIndex) {
paginationHandler();
var cuerrentElement = arrElement[iIndex],
dest = cuerrentElement.position();
initEffects(dest, cuerrentElement);
}
}
//绑定键盘事件
function keyDown() {
var keydownId;
win.keydown(function (e) {
clearTimeout(keydownId);
keydownId = setTimeout(function () {
var keyCode = e.keyCode;
if (keyCode == 37 || keyCode == 38) {
SP.moveSectionUp();
} else if (keyCode == 39 || keyCode == 40) {
SP.moveSectionDown();
}
}, 150);
});
}
})(jQuery);
demo.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>页面切换</title>
<script type="text/javascript" src="jquery-1.11.2.min.js"></script>
<script type="text/javascript" src="pageswitch.js"></script>
<script type="text/javascript">
$(function () {
$("#container").switchPage({ });
});
</script>
<style type="text/css">
h1, body, html
{
padding: 0;
margin: 0;
}
body
{
font-family: Arial, "Microsoft YaHei" , "Hiragino Sans GB" ,sans-serif;
}
html, body
{
height: 100%;
overflow: hidden;
} #container, .section
{
height: 100%;
position: relative;
}
div.section
{
background-color: #000;
background-size: cover;
background-position: 50% 50%;
}
#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/7.jpg);
}
#section4
{
background-image: url(images/5.jpg);
}
#section5
{
background-image: url(images/6.jpg);
}
#section6
{
background-image: url(images/4.jpg);
} .left
{
float: left;
} #pages
{
position: fixed;
right: 10px;
top: 50%;
list-style: none;
}
#pages li
{
width: 8px;
height: 8px;
border-radius: 50%;
background: #fff;
margin: 0 0 10px 5px;
}
#pages li.active
{
width: 14px;
height: 14px;
border: 2px solid #FFFE00;
background: none;
margin-left: 0;
}
</style>
</head>
<body>
<div id="container">
<div class="section active" id="section0">
</div>
<div class="section" id="section1">
</div>
<div class="section" id="section2">
</div>
<div class="section" id="section3">
</div>
<div class="section" id="section4">
</div>
<div class="section" id="section5">
</div>
<div class="section" id="section6">
</div>
</div>
</body>
</html>
Html 全屏切换效果的更多相关文章
- 【CSS3】使用CSS3制作全屏切换效果
在线演示: DEMO DEMO中及以下代码并没有写兼容代码,请使用高级浏览器打开,IE版本对CSS3支持并不太友好,IE11打开没有滚屏效果. 兼容代码前缀: -webkit- -moz- -o- - ...
- jquery简单的大背景banner图片全屏切换
详细内容请点击 这个是我初毕业刚进公司那会帮同事(同时也是同学)写的一个PC端的全屏图片切换效果,对于刚毕业的我来说写出来那会的喜悦之情是无法言表的,那时的我还是什么不懂的小白白,俗称菜鸟.个人网站上 ...
- jQuery插件开发——全屏切换插件
这个插件包含三个部分:HTML结构.CSS代码和JS代码. HTML结构是固定的,结构如下: <!--全屏滚动--> <div class="fullpage-contai ...
- 原生JS实现全屏切换以及导航栏滑动隐藏及显示——重构前
思路分析: 向后滚动鼠标滚轮,页面向下全屏切换:向前滚动滚轮,页面向上全屏切换.切换过程为动画效果. 第一屏时,导航栏固定在页面顶部,切换到第二屏时,导航条向左滑动隐藏.切换回第一屏时,导航栏向右滑动 ...
- jQuery鼠标滚动垂直全屏切换代码
体验效果:http://hovertree.com/texiao/jquery/68/ 源码下载:http://hovertree.com/h/bjaf/f643upc4.htm 代码如下: < ...
- js实现网页全屏切换(平滑过渡),鼠标滚动切换
实现效果为页面平滑过渡全屏切换,点击导航和鼠标滚动都可以切换. 效果图: html代码: <!DOCTYPE html> <html> <head lang=" ...
- 全屏滚动效果H5FullscreenPage.js
前提: 介于现在很多活动都使用了 类似全屏滚动效果 尤其在微信里面 我自己开发了一个快速构建 此类项目的控件 与市面上大部分控件不同的是此控件还支持元素的动画效果 并提供多种元素效果 基于zepto. ...
- HTML5实现网页的全屏切换
使用HTML5提供的JavaScript Api可以实现主流浏览器的全屏和退出全屏操作,封装成进入全屏和退出全屏的函数如下: //进入全屏 function enterFullScreen() { v ...
- pagePiling.js - 创建漂亮的全屏滚动效果
全屏滚动效果是最近非常流行的网页设计形式,带给用户良好的视觉和交互体验.pagePiling.js 这款 jQuery 插件可以帮助前端开发人员轻松实现这种效果.支持所有的主流浏览器,包括IE8+,支 ...
随机推荐
- Windows桌面开发者的必备软件
如果你新安装好了一台WindowsXP或者Windows7的机器,作为一个开发人员,我建议安装下面这些软件: 1,WinMerge. http://winmerge.org/ 2, Lua for w ...
- android 开发 制作弹出等待进度条
技术点: dialog:ProgressBar:animated-rotate: 弹出框: import com.carspeak.client.R; import android.app.Dialo ...
- 使用try-with-resources注意的问题
package coin; import java.io.FileInputStream; import java.io.ObjectInputStream; /** * 使用 try-with-re ...
- 圆形DIV
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" ...
- memcached使用说明
1.在服务器上注册服务 2.启动服务:services.msc 3.客户端创建服务接口 object Get(string key); List<string> GetKeys ...
- Leetcode#61 Rotate List
原题地址 我一直不太理解为什么叫rotate,翻译成"旋转"吧,似乎也不像啊.比如: 1->2->3->4->5->NULL 向右旋转2的距离,变成了 ...
- ios containerViewController
- (void)replaceViewController:(UIViewController *)existingViewController withViewController:(UIViewC ...
- SSH无密码验证
一.安装和启动SSH协议 sudo yum install ssh sudo yum install rsync service sshd restart 启动服务 (rsync是一个远程数据同步工具 ...
- jquery的ajax同步和异步的理解及示例
之前一直在写JQUERY代码的时候遇到AJAX加载数据都需要考虑代码运行顺序问题.最近的项目用了到AJAX同步.这个同步的意思是当JS代码加载到当前AJAX的时候会把页面里所有的代码停止加载,页面出去 ...
- MariaDB Galera Cluster 部署
原文 http://code.oneapm.com/database/2015/07/02/mariadb-galera-cluster/MariaDB作为Mysql的一个分支,在开源项目中已经广泛 ...