之前介绍了好多网页幻灯片。今天给大家带来一款基于TweenMax.js的网页幻灯片。这款幻灯片以不规则的碎片百叶窗的形式切换。切换效果非常漂亮。一起看下效果图:

在线预览   源码下载

实现的代码。

html代码:

 <div id="container">
</div>
<script src='delaunay.js'></script>
<script src='TweenMax.js'></script>
<script> // For more check out zachsaucier.com const TWO_PI = Math.PI * 2; var images = [],
imageIndex = 0; var image,
imageWidth = 768,
imageHeight = 485; var vertices = [],
indices = [],
prevfrag = [],
fragments = []; var margin = 50; var container = document.getElementById('container'); var clickPosition = [imageWidth * 0.5, imageHeight * 0.5]; window.onload = function() {
TweenMax.set(container, {perspective:500}); // images from http://www.hdwallpapers.in
var urls = [
'1.jpg',
'2.jpg',
'3.jpg',
'4.jpg'
],
image,
loaded = 0;
// very quick and dirty hack to load and display the first image asap
images[0] = image = new Image();
image.onload = function() {
if (++loaded === 1) { for (var i = 1; i < 4; i++) {
images[i] = image = new Image(); image.src = urls[i];
}
placeImage();
}
};
image.src = urls[0];
}; function placeImage(transitionIn) {
image = images[imageIndex]; if (++imageIndex === images.length) imageIndex = 0; var num = Math.random();
if(num < .25) {
image.direction = "left";
} else if(num < .5) {
image.direction = "top";
} else if(num < .75) {
image.direction = "bottom";
} else {
image.direction = "right";
} container.appendChild(image);
image.style.opacity = 0; if (transitionIn !== false) {
triangulateIn();
}
} function triangulateIn(event) {
var box = image.getBoundingClientRect(),
top = box.top,
left = box.left; if(image.direction == "left") {
clickPosition[0] = 0;
clickPosition[1] = imageHeight / 2;
} else if(image.direction == "top") {
clickPosition[0] = imageWidth / 2;
clickPosition[1] = 0;
} else if(image.direction == "bottom") {
clickPosition[0] = imageWidth / 2;
clickPosition[1] = imageHeight;
} else if(image.direction == "right") {
clickPosition[0] = imageWidth;
clickPosition[1] = imageHeight / 2;
} triangulate();
build();
} function triangulate() {
for(var i = 0; i < 40; i++) {
x = -margin + Math.random() * (imageWidth + margin * 2);
y = -margin + Math.random() * (imageHeight + margin * 2);
vertices.push([x, y]);
}
vertices.push([0,0]);
vertices.push([imageWidth,0]);
vertices.push([imageWidth, imageHeight]);
vertices.push([0, imageHeight]); vertices.forEach(function(v) {
v[0] = clamp(v[0], 0, imageWidth);
v[1] = clamp(v[1], 0, imageHeight);
}); indices = Delaunay.triangulate(vertices);
} function build() {
var p0, p1, p2,
fragment; var tl0 = new TimelineMax({onComplete:buildCompleteHandler}); for (var i = 0; i < indices.length; i += 3) {
p0 = vertices[indices[i + 0]];
p1 = vertices[indices[i + 1]];
p2 = vertices[indices[i + 2]]; fragment = new Fragment(p0, p1, p2); var dx = fragment.centroid[0] - clickPosition[0],
dy = fragment.centroid[1] - clickPosition[1],
d = Math.sqrt(dx * dx + dy * dy),
rx = 30 * sign(dy),
ry = 90 * -sign(dx),
delay = d * 0.003 * randomRange(0.9, 1.1);
fragment.canvas.style.zIndex = Math.floor(d).toString(); var tl1 = new TimelineMax(); if(image.direction == "left") {
rx = Math.abs(rx);
ry = 0;
} else if(image.direction == "top") {
rx = 0;
ry = Math.abs(ry);
} else if(image.direction == "bottom") {
rx = 0;
ry = - Math.abs(ry);
} else if(image.direction == "right") {
rx = - Math.abs(rx);
ry = 0;
} tl1.from(fragment.canvas, 1, {
z:-50,
rotationX:rx,
rotationY:ry,
scaleX:0,
scaleY:0,
ease:Cubic.easeIn
});
tl1.from(fragment.canvas, 0.4,{alpha:0}, 0.6); tl0.insert(tl1, delay); fragments.push(fragment);
container.appendChild(fragment.canvas);
}
} function buildCompleteHandler() {
// add pooling?
image.style.opacity = 1;
image.addEventListener('transitionend', function catchTrans() {
fragments.forEach(function(f) {
container.removeChild(f.canvas);
}); fragments.length = 0;
vertices.length = 0;
indices.length = 0; placeImage();
this.removeEventListener('transitionend',catchTrans,false);
}, false); } //////////////
// MATH UTILS
////////////// function randomRange(min, max) {
return min + (max - min) * Math.random();
} function clamp(x, min, max) {
return x < min ? min : (x > max ? max : x);
} function sign(x) {
return x < 0 ? -1 : 1;
} //////////////
// FRAGMENT
////////////// Fragment = function(v0, v1, v2) {
this.v0 = v0;
this.v1 = v1;
this.v2 = v2; this.computeBoundingBox();
this.computeCentroid();
this.createCanvas();
this.clip();
};
Fragment.prototype = {
computeBoundingBox:function() {
var xMin = Math.min(this.v0[0], this.v1[0], this.v2[0]),
xMax = Math.max(this.v0[0], this.v1[0], this.v2[0]),
yMin = Math.min(this.v0[1], this.v1[1], this.v2[1]),
yMax = Math.max(this.v0[1], this.v1[1], this.v2[1]); this.box = {
x:Math.round(xMin),
y:Math.round(yMin),
w:Math.round(xMax - xMin),
h:Math.round(yMax - yMin)
}; },
computeCentroid:function() {
var x = (this.v0[0] + this.v1[0] + this.v2[0]) / 3,
y = (this.v0[1] + this.v1[1] + this.v2[1]) / 3; this.centroid = [x, y];
},
createCanvas:function() {
this.canvas = document.createElement('canvas');
this.canvas.width = this.box.w;
this.canvas.height = this.box.h;
this.canvas.style.width = this.box.w + 'px';
this.canvas.style.height = this.box.h + 'px';
this.canvas.style.left = this.box.x + 'px';
this.canvas.style.top = this.box.y + 'px';
this.ctx = this.canvas.getContext('2d');
},
clip:function() {
this.ctx.save();
this.ctx.translate(-this.box.x, -this.box.y);
this.ctx.beginPath();
this.ctx.moveTo(this.v0[0], this.v0[1]);
this.ctx.lineTo(this.v1[0], this.v1[1]);
this.ctx.lineTo(this.v2[0], this.v2[1]);
this.ctx.closePath();
this.ctx.clip();
this.ctx.drawImage(image, 0, 0);
this.ctx.restore();
}
};//@ sourceURL=pen.js

css代码:

  body
{
background-color: #000;
margin:;
overflow: hidden;
} canvas
{
position: absolute;
backface-visibility: hidden;
-webkit-backface-visibility: hidden;
-moz-backface-visibility: hidden;
-ms-backface-visibility: hidden;
} img
{
position: absolute;
-webkit-transition: opacity .3s;
transition: opacity .3s;
} #container
{
position: absolute;
width: 768px;
height: 432px;
left:;
right:;
top:;
bottom:;
margin: auto;
}

注:本文爱编程原创文章,转载请注明原文地址:http://www.w2bc.com/Article/9438

一款基于TweenMax.js的网页幻灯片的更多相关文章

  1. 一款基于TweenMax跟随鼠标单击移动的div

    今天给大家分享一款基于TweenMax跟随鼠标单击移动的div.在这款实例中你可以单击任意位置,div会移动到你单击的位置.效果图如下: 在线预览   源码下载 实现的代码. html代码: < ...

  2. 基于SVG.js实现网页初始化线条描绘效果

    前端实现看到一个网页的效果很cool(参考https://tympanus.net/Development/SVGDrawingAnimation/index2.html),决定自己去实现以下这个效果 ...

  3. 基于React.js网页版弹窗|react pc端自定义对话框组件RLayer

    基于React.js实现PC桌面端自定义弹窗组件RLayer. 前几天有分享一个Vue网页版弹框组件,今天分享一个最新开发的React PC桌面端自定义对话框组件. RLayer 一款基于react. ...

  4. 基于node.js制作爬虫教程

    前言:最近想学习node.js,突然在网上看到基于node的爬虫制作教程,所以简单学习了一下,把这篇文章分享给同样初学node.js的朋友. 目标:爬取 http://tweixin.yueyishu ...

  5. 基于Three.js的360X180度全景图预览插件

    基于Three.js的360X180度全景图预览插件 时间 2015-08-12 10:01:10  HTML5中国 原文  http://www.html5cn.org/article-8621-1 ...

  6. 基于Ascensor.js全屏切换页面插件

    今天给大家分享一款基于Ascensor.js全屏切换页面插件,这款实例 适用浏览器:IE8.360.FireFox.Chrome.Safari.Opera.傲游.搜狗.世界之窗.效果图如下: 在线预览 ...

  7. 基于HTML5 Canvas的网页画板实现教程

    HTML5的功能非常强大,尤其是Canvas的应用更加广泛,Canvas画布上面不仅可以绘制任意的图形,而且可以实现多种多样的动画,甚至是一些交互式的应用,比如网页网版.这次我们要来看的就是一款基于H ...

  8. 基于Vue.js的Web视频播放器插件vue-vam-video@1.3.6 正式发布

    前言 今日正式发布一款基于Vue.js的Web视频播放器插件.可配置,操作灵活.跟我一起来体验吧! 线上地址体验 基于vue3.0和vue-vam-video,我开发了一款在线视频播放器. 网址: h ...

  9. 一款基于Bootstrap的js分页插件bootstrap-paginator使用实例

    Bootstrap Paginator是一款基于Bootstrap的js分页插件,功能很丰富.它提供了一系列的参数用来支持用户的定制,提供了公共的方法可随时获得插件状态的改变,以及事件来监听用户的动作 ...

随机推荐

  1. python学习笔记011——内置函数filter()

    1 描述 filter() 函数用于过滤序列,过滤掉不符合条件的元素,返回由符合条件元素组成的新列表. 2 语法 filter(function, iterable) function -- 函数,过 ...

  2. PLSQL_数据结构类型的解析(概念)

    2014-06-02 Created By BaoXinjian

  3. Linux时钟

    一.前言 时钟或者钟表(clock)是一种计时工具,每个人都至少有一块,可能在你的手机里,也可能佩戴在你的手腕上.如果Linux也是一个普通人的话,那么她的手腕上应该有十几块手表,包括:CLOCK_R ...

  4. 如何恢复 Linux删除的文件

    原理及普通文件的恢复 要想恢复误删除的文件,必须清楚数据在磁盘上究竟是如何存储的,以及如何定位并恢复数据.本文从数据恢复的角度,着重介绍了 ext2 文件系统中使用的一些基本概念和重要数据结构,并通过 ...

  5. iOS_21团购_Popover适应iPad横竖屏切换

    终于效果图: 代码片段: // // DockItemLocation.m // 帅哥_团购 // // Created by beyond on 14-8-13. // Copyright (c) ...

  6. Android图片处理:识别图像方向并显示

    在Android中使用ImageView显示图片的时候发现图片显示不正.方向偏了或者倒过来了. 解决问题非常自然想到的分两步走: 1.自己主动识别图像方向,计算旋转角度. 2.对图像进行旋转并显示. ...

  7. WinForm窗体键盘事件,支持方向键和回车键

    /// <summary> /// 快捷键操作 /// </summary> protected override bool ProcessCmdKey(ref Message ...

  8. Spring Cloud 通过代码自定义配置Ribbon

    我们还是先从官网文档开始学习,如下图所示,我们可以搞一个测试配置类,来验证是否真的可以通过代码来自定义配置Ribbon,但文档明确给出了警告:即这个测试配置类不能放在@ComponentScan所扫描 ...

  9. verilog 不可综合语句

    转自http://bbs.ednchina.com/BLOG_ARTICLE_1770084.HTM 基础知识:verilog 不可综合语句  (1)所有综合工具都支持的结构:always,assig ...

  10. [na]那些OVER的封装(pppoe/ppp/ipsec)

    什么over什么,如pppoe, ppp的封装都在over对象之后,入下图: PPPOE Ipsec