mass Framework flip插件

本文介绍了一个利用jQuery制作的Flip翻页效果插件,通过自定义的CSS和JavaScript实现了电子书式的翻页动画。该插件考虑到了不同浏览器的兼容性问题,并提供了水平和垂直两种翻转方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如果浏览器支持CSS3 transform 3D的话,我们可以玩许多东西,比如flip,即电子书软件的那种翻页效果。不过,像transform3D的高级东西,不是一般浏览器能玩转,更别提IE9了。因此这时轮到JS出马了。jQuery上素以插件多出名,在上面找了几个相关插件研究一翻,搞出了自己的flip插件。总共170行。

$.define("flip", "fx", function(){
    var flip = {
        begin: function() {
            var hyaline =  (!"1"[0] ? "#123456" : "transparent")//透明色,IE6不支持透明,因此要使用滤镜hack一下
            return {//初始化属性
                hyaline: hyaline,
                backgroundColor: hyaline,
                fontSize:0,
                lineHeight:0,
                borderTopWidth:0,
                borderLeftWidth:0,
                borderRightWidth:0,
                borderBottomWidth:0,
                borderTopColor: hyaline,
                borderBottomColor: hyaline,
                borderLeftColor: hyaline,
                borderRightColor: hyaline,
                background: "none",
                borderStyle: 'solid',
                height:0,
                width:0
            };
        },
        horizontal: function( obj ){
            var waist = obj.height /100 *25;
            var begin = flip.begin();
            begin.width= obj.width;//将初始属性克隆成三份,并逐渐改变它们
            return {
                hyaline: begin.hyaline,
                begin: begin,
                middle: {
                    borderTopWidth: 0,
                    borderBottomWidth: 0,
                    borderLeftWidth: waist,
                    borderRightWidth: waist,
                    borderTopColor: "#999",
                    borderBottomColor:"#999",
                    top:  obj.top + (obj.height/2),//向上移
                    left: obj.left - waist//向左移
                },
                end: {
                    borderBottomWidth: 0,
                    borderTopWidth: 0,
                    borderLeftWidth: 0,
                    borderRightWidth: 0,
                    borderTopColor: begin.hyaline,
                    borderBottomColor: begin.hyaline,
                    top: obj.top,
                    left: obj.left
                }
            };
        },
        vertical:  function ( obj ) {
            var waist = obj.height/100 * 25;
            var begin =  flip.begin();
            begin.height = obj.height;
            return {
                hyaline: begin.hyaline,
                begin : begin,
                middle : {
                    borderTopWidth: waist,
                    borderBottomWidth: waist,
                    borderLeftWidth: 0,
                    borderRightWidth: 0,
                    borderLeftColor: "#999",
                    borderRightColor: "#999",
                    top: obj.top-waist,
                    left: obj.left+ (obj.width/2)
                },
                end: {
                    borderTopWidth: 0,
                    borderLeftWidth: 0,
                    borderRightWidth: 0,
                    borderBottomWidth: 0,
                    borderLeftColor: begin.hyaline,
                    borderRightColor: begin.hyaline,
                    top: obj.top,
                    left: obj.left
                }
            };
        }
    }
    var dirMap = {
        t: "Top",
        b: "Bottom",
        l: "Left",
        r: "Right"
    }
    function getDirOption( obj, dir, orientation ){
        var result = flip[ orientation ]( obj ), arr = ["begin", "end"]
        dir.replace( $.rword, function( who ){
            var which = arr.shift();
            result[ which ]["border"+ who + "Width"] = obj[ orientation === "horizontal" ? "height" : "width"];
            result[ which ]["border"+ who + "Color"] = obj[which + "Bgc"];
        });
        return result;
    }
    //hash 中的主要参数 beginBgc endBgc before after frame, direction
    $.fn.flip = function(duration, hash){//并不是原对象进行动画,而是其克隆进行动画
        var props =  hash || duration || {}
        if(typeof duration === "function"){// fx(obj fn)
            hash = duration;               // fx(obj, 500, fn)
            duration = 500;
        }
        if(typeof hash === "function"){   //  fx(obj, num, fn)
            props.after = hash;           //  fx(obj, num, {after: fn})
        }
        var direction = props.direction || "tb";
        direction = dirMap[ direction.charAt(0) ] + ","+dirMap[ direction.charAt(1) ]
        duration = Math.floor( duration / 2 );
        var orientation = direction.charAt(1) === "o" ?  "horizontal" : "vertical" ;
        return this.each(function(){
            var $this = $(this);
            if($this.data('fliping')){
                return false;
            }
            $this.data("fliping", 1);
            //取得原有对象中的有用的信息
            var message = {
                width: $this.width(),
                height: $this.height(),
                beginBgc: hash.beginBgc || $this.css("bgc"),
                endBgc: hash.endBgc || "#999",
                top: $this.offset().top,
                left: $this.offset().left
            };
            //添加替身
            var $clone = $this.css("visibility","hidden")
            .clone(true)
            .data('fliping',1)
            .appendTo("body")
            .html("")
            .css({
                visibility: "visible",
                position: "absolute",
                left: message.left,
                top: message.top,
                margin: 0,
                zIndex: 9999,
                boxShadow:"0px 0px 0px #000"
            });
            var dirOption = getDirOption( message, direction, orientation);
            if(!"1"[0]){//fuck IE6
                dirOption.begin.filter = "chroma(color=" + dirOption.hyaline + ")";
            }
            var middle = dirOption.middle, end = dirOption.end, self = this;
            //绑定回调
            middle.before = function( clone, prop, fx ){
                if(typeof props.before === "function" ){
                    props.before.call( self, clone, prop, fx );
                }
            }
            middle.frame = end.frame = function( clone, prop, fx ){
                if(typeof props.frame === "function" ){
                    props.frame.call( self, clone, prop, fx );
                }
            }
            end.after = function( clone, prop, fx){
                $this.css("visibility", "visible").removeData('fliping');
                $clone.remove();
                if(typeof props.after === "function" ){
                    props.after.call( self, clone, prop, fx );
                }
            }
            $clone.css( dirOption.begin ).fx( duration, middle).fx( duration, dirOption.end );
        })
    }
})
// 2012.2.19 发布

下面就是完整例子:

IE下如果报错,请它刷新页面,再不行,下载回来看。

$.fn.flipflig 翻转效果 by 司徒正美
Hello! I'm a flip-box! :)
            $.require("ready,fx,event,attr,more/flip", function(){
                $("#flipPad a:not(.revert)").click(function(){
                    var btn = $(this);
                    $("#flipbox").flip(1000,{
                        direction: btn.attr("rel"),
                        endBgc: btn.attr("rev"),
                        hyaline:"fff",
                        after: function(){
                            $(this).html( btn.attr("title") )
                        }
                    })
                    return false;
                });
            })
            

运行代码

更详细的使用方法请到github,看它的文档

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值