jquery的animate关于background-position属性
jQuery 的 animate 虽然能直接使用 CSS 的方式来进行动画,但有些属性其实是不支持的,例如:background-position。
- 谷歌支持 background-position-x; background-position-y ;firefox不支持。
- 使用 background-position 插件
- 使用另外一个background-position插件:_this.animate({'background-position':'477px 0px'},750);
/* 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属性的更多相关文章
- jquery的animate({})动画整理
在网页制作的过程中少不了用到各种动画,形式多种多样,flash,css,js,canvas,等等都能实现,对于其优劣和效果只能说各有千秋. 什么是动画效果,其实网页中的渐变效果就是一种很基础的动画,动 ...
- jQuery之animate()用法
最近在学习jQuery,看到一个很有意思的函数animate(),但是在网上却没有查到相关的详细资料,于是打算参考jQuery API,自己总结一下. 概述 animate() 方法执行 CSS 属性 ...
- JQuery动画animate的stop方法使用详解
JQuery动画animate的stop方法使用详解 animate语法: 复制代码 代码如下: $(selector).animate(styles,speed,easing,callback) 复 ...
- jquery的api以及用法总结-属性/css/位置
属性/css 属性 .attr() attr()设置普通属性,prop()设置特有属性 获取或者设置匹配的元素集合中的第一个元素的属性的值 如果需要获取或者设置每个单独元素的属性值,需要依靠.each ...
- jQuery中Animate进阶用法(一)
jQuery中animate的用法你了解多少呢?如果仅仅是简单的移动位置,显示隐藏,哦!天哪你在浪费资源!因为animate太强大了,你可以有很多意想不到的用法!让我们一起研究一下吧~~ 首先要了解j ...
- 由position属性引申的关于css的进阶讨论(包含块、BFC、margin collapse)
写这篇文章的起因是源于这篇文章:谈谈面试与面试题 中关于position的讨论,文中一开始就说的这句话: 面试的时候问个css的position属性能刷掉一半的人这是啥情况…… 其实这问题我本来打算的 ...
- Css中的Position属性
Css中的Position属性 Css属性在线查询地址: http://www.css88.com/book/css/properties/index.htm CSS 中的 position 属性 在 ...
- DIV的Position属性和DIV嵌套DIV
1.前言 我们在利用div+css进行布局时,常常被div的位置弄的焦头烂额,很多人甚至放弃了div而直接用table.这里一如既往的推荐使用div布局,其实我们只要掌握了div的position属性 ...
- background复合属性详解(上):background-image
background复合属性是个很复杂的属性,花样非常多,比较神奇的是css3 中支持多图片背景了,这篇文章先讲讲background-image属性,其他背景属性会在后续的文章综合总结. 一.最基本 ...
随机推荐
- mysql 截取字符串
#select SUBSTRING_INDEX('1,2', ',', 1); SELECT * FROM dis_disease WHERE id = ( SELECT SUBSTRING_INDE ...
- RSA加密解密总结
简单的控制台程序 #include"stdafx.h" #include <math.h> #include<string.h> /*/求解密密钥d的函数( ...
- Android-工作总结-LX-2018-08-20-setHint
问题的因素: 调试了一下午,我一直是以为是数据没有传递过来,然后一直在数据获取环节检查在检查,数据跟踪在跟踪,最后发现数据获取是OK
- Maven Compilation error [package org.testng.annotations does not exist]
背景 在执行mvn test的时候,提示package org.testng.annotations does not exist 解决办法 Open pom.xml file. Go to &quo ...
- SQL Server中将多行数据拼接为一行数据并且有特殊字符
有表结构如下: 这时,要求显示所有学生的爱好的结果集,代码如下: ) as hobby FROM ( SELECT name, (SELECT hobby+',' FROM student WHERE ...
- 多彩浏览器win10版 隐私声明
(一)隐私保护 the app need internet access,we won't need your private information, in other words, your i ...
- (转)最全正则表达式总结:验证QQ号、手机号、Email、中文、邮编、身份证、IP地址等
什么是 RegExp? RegExp 是正则表达式(Regular expression)的缩写,作用是对字符串执行模式匹配. 通常用于格式验证.正则替换.查找子串等 各种编程语言的正则表达式基本相同 ...
- sqlite3简单操作
最近在操作公司视频设备的tutk转发服务器的时候,用到的数据库是sqlite,在此复习一下 目录 1 建立数据库档案 2 在sqlite3提示列下操作 3 SQL的指令格式 4 建立资料表 5 建立索 ...
- KindEditor的使用
KindEditor 是一套开源的在线HTML编辑器,主要用于让用户在网站上获得所见即所得编辑效果,开发人员可以用 KindEditor 把传统的多行文本输入框(textarea)替换为可视化的富文本 ...
- 【OCP-12c】CUUG 071题库考试原题及答案解析(13)
13.(6-7) choose twoWhich two statements are true regarding operators used with subqueries? (Choose t ...