jQuery 的 animate 虽然能直接使用 CSS 的方式来进行动画,但有些属性其实是不支持的,例如:background-position。

  1. 谷歌支持 background-position-x; background-position-y ;firefox不支持。
  2. 使用 background-position 插件
  3. 使用另外一个background-position插件:_this.animate({'background-position':'477px 0px'},750);
  4. /* http://keith-wood.name/backgroundPos.html
       Background position animation for jQuery v1.1.1.
       Written by Keith Wood (kbwood{at}iinet.com.au) November 2010.
       Dual licensed under the GPL (http://dev.jquery.com/browser/trunk/jquery/GPL-LICENSE.txt) and
       MIT (http://dev.jquery.com/browser/trunk/jquery/MIT-LICENSE.txt) licenses.
       Please attribute the author if you use it. */

    (function($) { // Hide scope, no $ conflict

    var BG_POS = 'bgPos';

    var usesTween = !!$.Tween;

    if (usesTween) { // jQuery 1.8+
        $.Tween.propHooks['backgroundPosition'] = {
            get: function(tween) {
                return parseBackgroundPosition($(tween.elem).css(tween.prop));
            },
            set: function(tween) {
                setBackgroundPosition(tween);
            }
        };
    }
    else { // jQuery 1.7-
        // Enable animation for the background-position attribute
        $.fx.step['backgroundPosition'] = setBackgroundPosition;
    };

    /* Parse a background-position definition: horizontal [vertical]
       @param  value  (string) the definition
       @return  ([2][string, number, string]) the extracted values - relative marker, amount, units */
    function parseBackgroundPosition(value) {
        var bgPos = (value || '').split(/ /);
        var presets = {center: '50%', left: '0%', right: '100%', top: '0%', bottom: '100%'};
        var decodePos = function(index) {
            var pos = (presets[bgPos[index]] || bgPos[index] || '50%').
                match(/^([+-]=)?([+-]?\d+(\.\d*)?)(.*)$/);
            bgPos[index] = [pos[1], parseFloat(pos[2]), pos[4] || 'px'];
        };
        if (bgPos.length == 1 && $.inArray(bgPos[0], ['top', 'bottom']) > -1) {
            bgPos[1] = bgPos[0];
            bgPos[0] = '50%';
        }
        decodePos(0);
        decodePos(1);
        return bgPos;
    }

    /* Set the value for a step in the animation.
       @param  tween  (object) the animation properties */
    function setBackgroundPosition(tween) {
        if (!tween.set) {
            initBackgroundPosition(tween);
        }
        $(tween.elem).css('background-position',
            ((tween.pos * (tween.end[0][1] - tween.start[0][1]) + tween.start[0][1]) + tween.end[0][2]) + ' ' +
            ((tween.pos * (tween.end[1][1] - tween.start[1][1]) + tween.start[1][1]) + tween.end[1][2]));
    }

    /* Initialise the animation.
       @param  tween  (object) the animation properties */
    function initBackgroundPosition(tween) {
        if (!usesTween) {
            var elem = $(tween.elem);
            var bgPos = elem.data(BG_POS); // Original background position
            elem.css('backgroundPosition', bgPos); // Restore original position
            tween.start = parseBackgroundPosition(bgPos);
        }
        tween.end = parseBackgroundPosition($.fn.jquery >= '1.6' ? tween.end :
            tween.options.curAnim['backgroundPosition'] || tween.options.curAnim['background-position']);
        for (var i = 0; i < tween.end.length; i++) {
            if (tween.end[i][0]) { // Relative position
                tween.end[i][1] = tween.start[i][1] + (tween.end[i][0] == '-=' ? -1 : +1) * tween.end[i][1];
            }
        }
        tween.set = true;
    }

    /* Wrap jQuery animate to preserve original backgroundPosition. */
    if (!usesTween) { // jQuery 1.7-
        $.fn.animate = function(origAnimate) {
            return function(prop, speed, easing, callback) {
                if (prop['backgroundPosition'] || prop['background-position']) {
                    this.data(BG_POS, this.css('backgroundPosition') || 'left top');
                }
                return origAnimate.apply(this, [prop, speed, easing, callback]);
            };
        }($.fn.animate);
    }

    })(jQuery);

jquery的animate关于background-position属性的更多相关文章

  1. jquery的animate({})动画整理

    在网页制作的过程中少不了用到各种动画,形式多种多样,flash,css,js,canvas,等等都能实现,对于其优劣和效果只能说各有千秋. 什么是动画效果,其实网页中的渐变效果就是一种很基础的动画,动 ...

  2. jQuery之animate()用法

    最近在学习jQuery,看到一个很有意思的函数animate(),但是在网上却没有查到相关的详细资料,于是打算参考jQuery API,自己总结一下. 概述 animate() 方法执行 CSS 属性 ...

  3. JQuery动画animate的stop方法使用详解

    JQuery动画animate的stop方法使用详解 animate语法: 复制代码 代码如下: $(selector).animate(styles,speed,easing,callback) 复 ...

  4. jquery的api以及用法总结-属性/css/位置

    属性/css 属性 .attr() attr()设置普通属性,prop()设置特有属性 获取或者设置匹配的元素集合中的第一个元素的属性的值 如果需要获取或者设置每个单独元素的属性值,需要依靠.each ...

  5. jQuery中Animate进阶用法(一)

    jQuery中animate的用法你了解多少呢?如果仅仅是简单的移动位置,显示隐藏,哦!天哪你在浪费资源!因为animate太强大了,你可以有很多意想不到的用法!让我们一起研究一下吧~~ 首先要了解j ...

  6. 由position属性引申的关于css的进阶讨论(包含块、BFC、margin collapse)

    写这篇文章的起因是源于这篇文章:谈谈面试与面试题 中关于position的讨论,文中一开始就说的这句话: 面试的时候问个css的position属性能刷掉一半的人这是啥情况…… 其实这问题我本来打算的 ...

  7. Css中的Position属性

    Css中的Position属性 Css属性在线查询地址: http://www.css88.com/book/css/properties/index.htm CSS 中的 position 属性 在 ...

  8. DIV的Position属性和DIV嵌套DIV

    1.前言 我们在利用div+css进行布局时,常常被div的位置弄的焦头烂额,很多人甚至放弃了div而直接用table.这里一如既往的推荐使用div布局,其实我们只要掌握了div的position属性 ...

  9. background复合属性详解(上):background-image

    background复合属性是个很复杂的属性,花样非常多,比较神奇的是css3 中支持多图片背景了,这篇文章先讲讲background-image属性,其他背景属性会在后续的文章综合总结. 一.最基本 ...

随机推荐

  1. Linux的进程与服务(一)

    启动的配置文件/etc/inittab,修改完配置文件以后 init q立即生效 # Default runlevel. The runlevels used by RHS are: # - halt ...

  2. 【笔记】metasploit渗透测试魔鬼训练营-信息搜集

    exploit 漏洞利用代码 编码器模块:免杀.控制 help [cmd] msfcli适合对网络中大量系统统一测试. 打开数据包路由转发功能:/etc/sysctl.conf /etc/rc.loc ...

  3. 从Objective-C到Swift,你必须会的(一)#pragma mark

    在Objective-C里,为了让代码组织的有序也方便用control+6的快捷键在Xcode中查找,所以出现了一个大家都很熟悉的东东.这就是:#prama mark. #pragma mark  但 ...

  4. visual studio 2013 git 记住密码

    原有配置: C:\Users\Administrator 下.gitconfig内容为 [user] name = lijf4 email = lijf4@lenovo.com 删除,修改为 [cre ...

  5. ZOJ3775 ?(>_o)! 2017-04-13 23:37 110人阅读 评论(0) 收藏

    ?(>_o)! Time Limit: 2 Seconds      Memory Limit: 65536 KB ?(>_o)! is a pseudo-object-oriented ...

  6. IIS隐藏网站

    IIS隐藏网站 1.站点建立一个文件夹:Test 2.在F盘新建Web文件夹(放要隐藏的网站) 3.右键Test文件夹-新建虚拟目录,虚拟目录指向步骤2 4.删除Test文件夹即可

  7. centos7 二进制安装mysql5.6

    下载mysqltar包 wget -q http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.35-linux-glibc2.5-x86_64.tar.g ...

  8. Java50道经典习题-程序12 计算奖金

    题目:企业发放的奖金根据利润提成.利润(I)低于或等于10万元时,奖金可提10%:    利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.5%:     ...

  9. excel冻结窗口

    Excel中如果输入内容太多,希望滚动时候,可以看到表头,需要冻结表格.如果我们选中C3表格,执行冻结操作,会看到C3左边和上边有两条直线,这样C3的左边和上边的表格都没法变动,因此冻结表格只能冻结首 ...

  10. robot framework学习笔记之十一--第三方库requests详解

    一.安装 Requests 通过pip安装 pip install requests 或者,下载代码后安装: $ git clone git://github.com/kennethreitz/req ...