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. 一个GIS系统需具备的功能

    数据的存储(数据结构) 图形显示 功能:拓扑分析 开源的GIS系统:QGIS,SharpMap 各个功能组件

  2. 《Forward团队-爬虫豆瓣top250项目-开发文档》

    码云地址:https://github.com/xyhcq/top250 模块功能:获取豆瓣top250网页的源代码,并分析. def getHTMLText(url,k): # 获取网页源代码 tr ...

  3. mysql - json串新增字段

    1.建表 -- 建表 drop table if exists ta_product2; CREATE TABLE ta_product2( id int primary key auto_incre ...

  4. Centos 7 安装 mysql5.7

    1.需要下载mysql 下载地址:http://dev.mysql.com/downloads/mysql/ 2.将下载的rpm包上传到centos 7上(我是放在根下面的opt目录) 3. 安装my ...

  5. docker把系统盘占满后的恢复

    docker报[Error response from daemon: Error running DeviceCreate (createSnapDevice) dm_task_run failed ...

  6. byte[] 数组和字符串的转换,与byte[] 数组和int类型的之间的转化

    我们先来看看byte bool  int ushort  等的定义 首先时byte[]数组与string之间的转换 string 转换位byte[] 数组 string str = "1-1 ...

  7. .Net MVC5路由机制与扩展

    新建一个MVC项目启动后,首先访问的地址是http://localhost:xxx/Home/Index,这时候我们也明白因为在程序中有个叫做Home的控制器,并且在这个控制器下面有个叫做Index的 ...

  8. WPF 使用QRCoder生成二维码

    vs中使用Nuget获取QRCoder 窗体中添加按钮和Iage <Window x:Class="QRCoderTest.MainWindow" xmlns="h ...

  9. maven的jar路径、下载路径

    jar路径:在localRepository中填写先要放的位置 下载镜像:添加新的就可以 <mirror> <id>nexus-aliyun</id> <mi ...

  10. G - 確率(水题)

    原文链接 G - 確率 Time Limit:1000MS     Memory Limit:131072KB     64bit IO Format:%lld & %llu Submit S ...