动态 CSS 样式和动画 - Jquery 插件





5.00/5 (4投票s)
Jquery 插件,用于创建动态 CSS 动画和过渡效果。
下载
您可以从 github 下载源代码。
引言
CSS 脚本在为 HTML DOM 对象设置样式和设置动画方面非常酷。您可以将相同的样式应用于多个元素,从而创建出色的网页。但是,将这些样式应用于多个对象时,如果没有创建另一个 CSS 类,就没有办法应用特定于对象的特定值。
/*creating class for some objects*/
.style1{
width : 100%;
}
/*creating another class for another object to change one property*/
style2{
width : 50%;
}
LESS 和 SASS(CSS 预处理器)试图通过创建变量、继承和其他预处理器代码优化来解决一些问题,但最终结果是添加到网页的静态类型的 CSS 脚本。
问题
(直到现在)没有办法在 CSS 中创建特定于元素的特定值,因为它只有一种方式(选定的元素会受到类内属性的影响)。但您无法为每个元素生成特定值。
解决方法
如果我们想创建特定于元素的特定值,我们可以使用以下方法:
- 创建多个 CSS 类。
- 使用预处理器
- 将内联样式应用于元素的 style 属性。
- 使用 JavaScript 更改样式属性。
- 使用 jquery 将样式应用于目标元素。
但是……
当变体数量较少时,创建多个 CSS 类是可行的,但当有 10、20 个……或更多元素时,您将沉浸在具有相同功能的类中:( :( 。
内联样式对于仅一个与其他对象不同的对象来说是可行的。而且,您还失去了选择器的强大功能。
JavaScript 本身很难处理选择器和为每种浏览器编写前缀。
在我看来,jquery 是最适合这项工作的工具。它内置了选择器系统,而且它是一个 JavaScript 库,所以您处于计算环境中,可以处理元素,从而使元素能够影响 CSS 样式值,样式也能影响元素布局,而且它还有一个跨浏览器的 .css 属性,消除了前缀的麻烦。
动态 CSS 插件
在开始编写代码之前,需要注意的是,jquery 可以为选定的元素生成 CSS 值,但这很困难且需要管理,而这正是 Dynamic-CSS 所做的。
下载和安装
- 在您的 IDE 中创建一个新项目或新文件
- 添加 index.html
- 下载 jquery
- 在 index.html 中包含脚本
- 下载插件
- 在 index.html 中包含
- 添加一个新的空
- 添加一些 HTML 元素和 CSS 样式。
index.html 文件可能类似于下载包中的 index.html 文件
开始使用动态 CSS
使用 jquery 将 CSS 属性应用于元素
$('#container').children().filter('div').css(
{
transform : 'translate(0%,0%) scale(.5,.5)',
transition : 'all .5s .1s linear'
});
这是一个简单的查询,它将一些静态值应用于所有选定的元素。
现在,要使此属性动态化,我们将替换以下内容:
.css ----> .dcss /*动态 CSS 函数*/
-----------------------------------------------------------
transition : 'all .1s
将".1"值替换为
-----------------------------------------------------------
.dcss 接受 2 个参数,所以我们将添加另一个参数来定义
{
delay : .1
}
代码
//example1
$('#container').children().filter('div').dcss(
{
transform : 'translate(0%,0%) scale(.5,.5)',
transition : 'all .5s <delay>s linear'
},
{
delay : .1
});
现在,插件将读取 delay 的值,并将其应用于提供的 CSS 属性中
到目前为止,它只是样式和值的分离,但是如果我们用一个返回动态值的函数替换 ".1" 值,代码将如下所示:
//example2
$('#container').children().filter('div').dcss(
{
transform : 'translate(0%,0%) scale(.5,.5)',
transition : 'all 1s <delay>s linear'
},
{
delay : function(i,el,c){ return i * .1;}
});
};
现在,对于数组中的每个对象,插件将调用 "delay" 函数,并用生成的值替换每个
被调用函数的参数(i ,el , c)
i = Jquery 元素数组中目标元素的索引。
el = 元素本身。
c = 选定数组的长度。
这 3 个参数将传递给函数,以便进行计算并返回特定于 "el" 元素的值。
在这个例子中,我们返回 ( i * .1)
这意味着每个元素的延迟比前一个元素多 0.1 秒。
添加多个变量
//example 3
$('#container').children().filter('div').dcss(
{
transform : 'translate(<transX>%,<transY>%) scale(.5,.5)',
transition : 'all 1s <delay>s linear'
},
{
delay : function(i,el,c){ return i * .1;},
transX : function(){ return 50 - Math.random()*100;},
transY : function(){ return 50 - Math.random()*100;}
});
transX 函数返回一个从 -50 -> 50 的值,transY 也同样应用于 "transform" 属性。
需要注意的是,所有变量都是为每个元素计算一次。所以我们分开了 transX 和 transY。
变量的多重出现
//example 4
$('#container').children().filter('div').dcss(
{
width : '<len>px',
height : '<len>px'
},
{
len : function(i,el,c){return 30 + Math.random() * 100;}
});
};
在这里,我们使用 len 函数调整了所有元素的大小。
正如我们之前解释的,'len' 只会被计算一次,所以宽度 = 高度,所有结果都将是随机大小的正方形。
调用关键帧
修改关键帧是动态 CSS 的局限性之一,我们正在尝试修复它。它只能修改 CSS 样式表中的值,而不能修改。我们只能使用它们。
CSS 关键帧
//example 5
@-webkit-keyframes test{
0%{
background : red;
}
50%{
background:green;
}
100%{
background : blue;
}
};
JavaScript
$('#container').children().filter('div').dcss(
{
animation : 'test <dur>s 0s ease-in-out alternate infinite'
},
{
dur : function(i,el,c){return 1 + Math.random() * 5;},
});
这将为每个元素生成不同的持续时间,使用相同的关键帧。
字符串值
//example 6
$('#container').children().filter('div').dcss(
{
height : '400px',
transition : 'all 1s 0s ease<in><out>'
},
{
in : function(i,el,c){return (i % 2 == 0) ? '-in':''; },
out : function(i,el,c){return (i % 2 == 1) ? '-out':''; }
});
};
如果 "i" 是偶数,我们将添加 "-in",否则添加 "-out",所以
- 偶数元素将具有 "ease-in" 功能。
- 奇数元素将具有 "ease-out" 功能。
网格函数
以上所有插图都是动态 CSS 插件的基本用法。网格函数将把代码的可用性提升到一个新的水平。
网格**数学上**将一维数组转换为二维数组,包含行和列。
"grid" 函数根据当前元素与给定中心点的距离返回一个新的索引。
"gridRowCol" 函数返回给定索引的当前行和列。
函数
dcss.grid(index,count,cols , centerX , centerY,invert);
index = 当前元素的索引。
count = 网格单元的数量
cols = 网格列的数量
centerX,centerY = 网格的中心点,值从 0 到 1。
invert = 反转第一个元素使其成为最后一个,反之亦然。
用法
假设我们有一个 5 x 5 的
//example 7
$('#container').children().filter('div').dcss(
//css properties to be processed and applied to object
{
pointerEvents:'none',
borderRadius:'100px',
transform : 'translate(0%,0%) scale(.5,.5)',
opacity : '0',
transition : 'all 1s <time>s ease-in'
},
{
//values to be applied to property values
time : function(i,el,c){
var ii = dcss.grid(i,c,5,0,1);
return ii * .1;
}
}
);
ii = 为虚拟网格中的当前元素生成的新索引。
dcss.grid(i,c,5,0,1,false)
i = 当前元素的真实索引。
c = 25 /* 网格单元的数量*/
0,1 = 网格的中心点(设置为底部,左侧);
ii * .1 ==> 对于网格中的每个项目,延迟增加 0.1 秒
这里有些东西
如果我们从左上角开始计算,那么网格单元 2,3 和 3,2 将具有相同的距离,因此具有相同的网格索引。
这将创建流畅的动画。
上面的代码将隐藏网格元素并带有过渡效果。以下代码将以不同的参数再次显示它。
//example 7
$('#container').children().filter('div').dcss(
//css properties to be processed and applied to object
{
pointerEvents:'all',
borderRadius:'20px',
transform : 'translate(0%,0%) scale(1,1)',
opacity : '1',
transition : 'all .5s <time>s ease-out',
display:'block'
},
{
//values to be applied to property values
time : function(i,el,c){
return dcss.grid(i,c,5,.5,1) * .1;
}
});
纯垂直或水平动画
中心点会影响垂直和水平方向的单元格。
要禁用一个方向的动画渐变,只需将 null 传递而不是 0.0-1.0 值。
dcss.grid(i,c,5,0,null);
同一列中的所有元素将具有相同的索引,因此具有相同的延迟。
//example 8
$('#container').children().filter('div').dcss(
//css properties to be processed and applied to object
{
pointerEvents:'none',
borderRadius:'100px',
transform : 'translate(0%,0%) scale(.5,.5)',
opacity : '0',
transition : 'all 1s <time>s ease-in'
},
{
//values to be applied to property values
time : function(i,el,c){
var ii = dcss.grid(i,c,5,0,null);
return ii * .1;
}
}
);
反转参数
由于网格首先选择靠近中心点的元素,所以如果我们设置中心点为 0.5,0.5,第一个元素将是中心元素,它将始终比其他元素先开始动画。将 "invert" 参数设置为 true 将先选择最远的元素,然后是较近的元素,直到中心元素成为最后一个被选中的元素。
正常动画
//example 9
function showNormal(){
$('#container').children().filter('div').dcss(
//css properties to be processed and applied to object
{
pointerEvents:'all',
borderRadius:'20px',
transform : 'translate(0%,0%) scale(1,1)',
opacity : '1',
transition : 'all 1s <time>s ease-out',
display:'block'
},
{
//values to be applied to property values
time : function(i,el,c){
return dcss.grid(i,c,5,.5,.5,false) * .1;
}
});
}
并启用 "Invert"
//example 9
function showInvert(){
$('#container').children().filter('div').dcss(
//css properties to be processed and applied to object
{
pointerEvents:'all',
borderRadius:'20px',
transform : 'translate(0%,0%) scale(1,1)',
opacity : '1',
transition : 'all 1s <time>s ease-out',
display:'block'
},
{
//values to be applied to property values
time : function(i,el,c){
return dcss.grid(i,c,5,.5,.5,true) * .1;
}
});
}
玩转 3D:
我们将为容器创建一个 dcss 调用,为网格创建另一个调用。
//example 10
//animating container element
$('#container').dcss({
transform : 'perspective( 600px ) rotateY(<ang>deg)',
transition : 'all 2s 0s ease-in-out',
transformStyle: 'preserve-3d'
},
{
ang : function(){
var rnd = Math.random() * 360;
//if (rnd > 90) rnd +=180 ;
return -180 + rnd;
},
});
//animating grid elements
$('#container').children().filter('div').dcss(
//css properties to be processed and applied to object
{
pointerEvents:'none',
transform : 'translate3d(0px,0px,<z>px) scale(1,1)',
opacity : '0',
transition : 'all <time>s <delay>s ease-in-out'
},
{
//values to be applied to property values
z : function(i){
return -200 + Math.random() * 400;
},
delay : function(i){
return Math.random() * .5;
},
time : function(i){
return .5 + Math.random() * .5;
}
}
);
其他 dcss 函数
其他函数返回用于自定义使用的值。
dcss.gridRowCol(index,cols);
其中
index = 当前元素的真实索引。
cols = 网格中的列数。
返回具有 .row 和 .col 属性的对象,提供网格位置。
Ex
将背景位置设置为每个元素,以获得图像的碎片效果。
//example 11
//setup background image
$('#container').children().filter('div').dcss(
//css properties to be processed and applied to object
{
background: 'url(../images/1.jpg)',
backgroundPosition: '<l>% <t>%',
backgroundSize: '500% 500%'
},
{
l : function(i,el,c){
var rc = dcss.gridRowCol(i,5);
return rc.col / (5-1) * 100;
},
t : function(i,el,c){
var rc = dcss.gridRowCol(i,5);
return rc.row / (5-1) * 100;
}
});
普通图像
动画动态应用于每个网格单元
dcss.ease(s2,s3,r);
基本缓动函数接受 2 个控制浮点值和比例值。
关注点
优点
- 使用缓动自定义 CSS 属性
- 应用每个元素的样式值。
- 按需计算值
- 灵活性高,因为您仍然可以使用关键帧规则和类。
- 只需在页面中添加几行代码
- 没有性能负担。
缺点
- 使用 jquery 选择器,因此您会丢失对新添加元素的自动选择。
- 仍然无法更改关键帧值。
记住:
此插件并非旨在取代 CSS、LESS 或 SASS。该插件的目的是解决一些 CSS 的局限性。
幕后
专业人士可能很容易猜测插件的工作原理。这个想法本身非常简单。插件所做的只是将 替换为 var。
在阅读插件代码之前,请在此处阅读如何创建基本的 jquery 插件:here。
函数只有 1 个文件,分为 2 部分
- Jquery 插件
- dcss 对象包含辅助函数。
Jquery 函数
if(jQuery){
(function ( $ ) {
// replace all occurrence of 'token' inside 'text' by 'value'.
var replaceAll=function(text,token,value){
var index=-1;
string = text;
while((index = string.indexOf(token,index+1)) > -1){
string = string.replace(token, value);
};
return string;
}
//register our 'dcss' function as jquery plugin.
$.fn.dcss = function(props,values) {
//holder for modified property values
var p;
//hold property value till modifing all args
var v;
//count of elements
var c = this.length;
this.each(function(index){
//create new object to hold specific values for the current selected element
p = {};
//compute values
var vals = {};
for (var vkey in values) {
//get current value object {value : 1,added : 1}.
v = values[vkey];
//process the value according to its type
if ( typeof(v) === 'number'){
vals[vkey] = v;
}else if ( typeof(v) === 'function'){
vals[vkey] = v(index,this,c);
}else if ( typeof(v) === 'string'){
vals[vkey] = v;
}
}
//loop throw properties to change values inside them
for (var pkey in props) {
//set pre-processed value to holder 'p' object.
p[pkey] = props[pkey];
//loop throw all actual value to change them in p object.
for (var vkey in values) {
//replace all occurence of keys in the property value.
p[pkey] = replaceAll(p[pkey],'<'+ vkey + '>', vals[vkey] );
}
}
//apply the css.
$(this).css(p);
});
//return jquery object again to allow Chaining.
return this;
};
}( jQuery ));
}
dcss 对象
dcss = {
grid : function (index,count,cols , centerX , centerY,invert){
var row = Math.floor( index/ cols);
var col = (index - (row * cols));
var rows = Math.ceil(count/cols);
if(centerX != null){
col = col - (cols-1) * centerX;
}else
{
cols = 0;
col = 0;
}
if(centerY != null){
row = row - (rows-1) * centerY;
}else{
row = 0;
rows = 0;
}
if(row < 0) row = -row;
if(col < 0) col = -col;
var res = row+col;
if(invert ===true){
res = (cols+rows-2) -res;
}
return res ;
},
gridRowCol: function(index,cols){
var row = Math.floor( index/ cols);
var col = (index - (row * cols));
return {row:row,col:col};
},
ease : function ( c1,c2, r) {
var s2,s3;
s2 = c1;
s3 = c2;
var s1;
s1 = 0 + (s2 - 0) * r;
s2 = s2 + (s3 - s2) * r;
s3 = s3 + (1 - s3) * r;
s1 = s1 + (s2 - s1) * r;
s2 = s2 + (s3 - s2) * r;
return s1 + (s2 - s1) * r;
}
};
反馈
请随时对本文提供任何反馈;很高兴看到您的评论和投票。如果您有任何问题,请随时在此处提问。