JavaScript 图书翻页效果 - 20Things_PageFlip_Example

如果有看过Google所设计的网站<关于浏览器和网络的 20 项须知>,一定会喜欢上那仿真的翻页效果.

不少技术博客都说该网站的源码放出来了:http://code.google.com/p/20thingsilearned/

但却找不到下载的链接.

经过苦苦寻找,终于在html5rocks上面找到了.

下载下来测试一下之后,多少有点失望,功能上相比20thingsilearned,实在弱太多了.

不过源码写得十分简洁和工整,有详细的备注,再配合html5rocks的tutorial,很快就能弄懂源码,就可以自己去定制不同的风格了.

但在使用的过程中,发现了一个翻页时的小Bug.

所以干脆自己改了一下源码,顺便放上来让大家试用和修改.

下载地址:https://github.com/jiancm2011/20Things_PageFlip_Example

Demo:http://maplejan.com/codelaboratory/code/html5/20Things_PageFlip_Example


本次修改,主要解决了翻页时变量page计算错误的问题.

加入了一个新变量 direction 用来判断鼠标翻页的方向.

var direction = ""; //判断鼠标翻页的方向(Record the mouse position)


并对下面两个函数进行了一点修改.


	function mouseDownHandler( event ) {
		// Make sure the mouse pointer is inside of the book
		if (Math.abs(mouse.x) < PAGE_WIDTH) {
			if (mouse.x < 0 && page - 1 >= 0) {
				// We are on the left side, drag the previous page
				flips[page - 1].dragging = true;	
				direction = "left";
			}
			else if (mouse.x > 0 && page + 1 < flips.length) {
				// We are on the right side, drag the current page
				flips[page].dragging = true;
				direction = "right";
			}
		}
		// Prevents the text selection
		event.preventDefault();
	}
	
	function mouseUpHandler( event ) {
		for( var i = 0; i < flips.length; i++ ) {
			// If this flip was being dragged, animate to its destination
			if( flips[i].dragging ) {
				// Figure out which page we should navigate to
				if( mouse.x < 0 ) {
					flips[i].target = -1;
					if(direction == "right") {
						page = Math.min( page + 1, flips.length );
					}
				}
				if(mouse.x > 0){
					flips[i].target = 1;
					if(direction == "left") {
						page = Math.max( page - 1, 0 );
					}
				}
			}
			//console.log("page:" + page + ";  flips[" + i + "].target:" + flips[i].target + ";  flips[" + i + "].dragging:" + flips[i].dragging);
			flips[i].dragging = false;
		}
		//console.log(" ");
	}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值