如何创建不透明度渐变()?(对jQuery的改进)
How to create an opacity fade()? ( improvement over jQuery)
我没有使用JQuery,但我知道他们有这样的功能。我知道如何在JQuery中做到这一点,但我想要一个纯js解决方案。
如何更改CSS不透明度设置的时间?unix时间戳的精度为1000毫秒。。。所以这可能是一种方式。
使用clearTimeout和setTimeout是另一种方法。
最好的方法是什么。我试着查看JQuery源代码,但无法弄清楚它们到底在为fadeIn
和fadeOut
做什么。
相关
如何消除不透明渐变中未使用的参数?
这里有一个使用setTimeout的动画函数。你可以在这里看到它的工作原理:http://jsfiddle.net/jfriend00/GcxdG/.
function toggleOpacity(id) {
var el = document.getElementById(id);
if (el.style.opacity == 1) {
fadeObject(el, 1, 0, 2000)
} else {
fadeObject(el, 0, 1, 2000)
}
}
function fadeObject(el, start, end, duration) {
var range = end - start;
var goingUp = end > start;
var steps = duration / 20; // arbitrarily picked 20ms for each step
var increment = range / steps;
var current = start;
var more = true;
function next() {
current = current + increment;
if (goingUp) {
if (current > end) {
current = end;
more = false;
}
} else {
if (current < end) {
current = end;
more = false;
}
}
el.style.opacity = current;
if (more) {
setTimeout(next, 20);
}
}
next();
}
注意:这还不适用于不响应opacity
样式并且需要通过IE特定的filter
设置来设置不透明度的旧IE版本。
for (var i = 1; i < 100; i += 1) { // change the += 1 for different smoothness
(function(i) {
setTimeout(function() {
el.style.opacity = (100 - i) * 0.01;
}, i * 10);
})(i);
}
/**
** SEffects - user can set the opacity fade to up or down and the specefied time
*/
var SEffects = function ( element ) {
this.element = element;
};
SEffects.prototype.fade = function( direction, max_time ) {
var element = this.element;
element.elapsed = 0;
clearTimeout( element.timeout_id );
function next() {
element.elapsed += 10;
if ( direction === 'up' ) {
element.style.opacity = element.elapsed / max_time;
}
else if ( direction === 'down' ) {
element.style.opacity = ( max_time - element.elapsed ) / max_time;
}
if ( element.elapsed <= max_time ) {
element.timeout_id = setTimeout( next, 10 );
}
}
next();
};
相关文章:
- jQuery淡入淡出/动画不透明度到不同的显示类型,然后阻止
- jQuery动画仅在动画期间更改不透明度
- JQuery UI - 滑块更改幻灯片上的不透明度 - 错误
- JQuery 悬停函数不透明度动画
- 如何使用这个jQuery来实现css的不透明度
- jquery 淡入淡出更改内容并淡出不透明度
- JQuery 自定义灯箱 - 不透明度淡入淡出
- jQuery滑块不透明度错误仅在谷歌浏览器上
- jQuery/JavaScript 函数来更改样式不透明度
- 逐渐降低不透明度,因为元素是通过jQuery UI .draggable()拖动的
- jQuery 绑定以切换元素的不透明度
- 将不透明度层添加到带有侧边栏的HTML部分.js / jQuery
- 使用jQuery的选项卡不透明度
- Jquery Slider UI,用于在基于不透明度的OpenLayers中显示覆盖层
- jQuery翻转不透明度更改
- 使用Jquery更改拖动过程中元素的不透明度
- 如何创建不透明度渐变()?(对jQuery的改进)
- jQuery将鼠标悬停在“不透明度”上,然后单击“保持不透明度”
- 是否有其他的jquery不透明度选项
- 帮助Jquery不透明度