2014-02-21 181 views
1

所以,我知道jQuery使這超級簡單,但我想弄清楚如何做到這一點沒有一個js庫。我怎樣才能將淡入/淡出效果融入到這個滑塊中的圖像中?我知道你可以使用點符號來改變不透明度,但我無法弄清楚它的全部邏輯。添加淡入淡出效果到img滑塊 - 純粹的JavaScript

var myImg = document.querySelector('.imgSlot'), 
    myImgArray = ["images/x.jpg", "images/y.jpg", "images/z.jpg", "images/abc.jpg"], 
    imgIndex = 0, 
    varTimerSpeed = 4000, 
    intervalHandle = setInterval(changeImg, varTimerSpeed); 

function changeImg() { 
    myImg.setAttribute('src', myImgArray[imgIndex]); 
    imgIndex++; 
    if (imgIndex >= myImgArray.length) { 
     imgIndex = 0; 
    } 
}; 

任何建議,將不勝感激,

大衛

回答

0

通過使用動畫庫做到這一點。它使用setTimeout或setInterval函數每隔幾毫秒將更改應用到對象的樣式,從而生成動畫。 例如:

function moveElementToRight(el) { 
    setInterval(function() { 
     el.style.marginLeft = el.style.marginLeft + 10 //increase marginLeft by 10 
    }),20) //every 20 milliseconds 
} 

一些基本邏輯:

var interval = 20 //milliseconds. Each 20 milliseconds = new frame. 
var duration = 500 //milliseconds. Total time for the animation 
var frames = Math.ceil(duration/interval) // frames count for the animation 

只要通過使用具有緩動函數此值,這將返回一個值用於當前幀(區間* I ++,from_value,to_value ,持續時間);這裏「我」是通過循環幀數獲得的。 From_value和to_value是元素的樣式起始值和元素的樣式結束值(這也可以應用於不透明度,這會導致漸變效果)。

這是非常粗略的例子,但這是主要原則。爲了緩解影響,您可以使用緩動功能。這裏有一些:

easeInQuad: function (t, b, c, d) { 
     return c*(t/=d)*t + b; 
    }, 
    easeOutQuad: function (t, b, c, d) { 
     return -c *(t/=d)*(t-2) + b; 
    }, 
    easeInOutQuad: function (t, b, c, d) { 
     if ((t/=d/2) < 1) return c/2*t*t + b; 
     return -c/2 * ((--t)*(t-2) - 1) + b; 
    }, 
    easeOutCubic: function (t, b, c, d) { 
     return c*((t=t/d-1)*t*t + 1) + b; 
    }, 
    easeInOutCubic: function (t, b, c, d) { 
     if ((t/=d/2) < 1) return c/2*t*t*t + b; 
     return c/2*((t-=2)*t*t + 2) + b; 
    }, 
    easeInQuart: function (t, b, c, d) { 
     return c*(t/=d)*t*t*t + b; 
    }, 
    easeOutQuart: function (t, b, c, d) { 
     return -c * ((t=t/d-1)*t*t*t - 1) + b; 
    }, 
    easeInOutQuart: function (t, b, c, d) { 
     if ((t/=d/2) < 1) return c/2*t*t*t*t + b; 
     return -c/2 * ((t-=2)*t*t*t - 2) + b; 
    }, 
    easeInQuint: function (t, b, c, d) { 
     return c*(t/=d)*t*t*t*t + b; 
    }, 
    easeOutQuint: function (t, b, c, d) { 
     return c*((t=t/d-1)*t*t*t*t + 1) + b; 
    }, 
    easeInOutQuint: function (t, b, c, d) { 
     if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b; 
     return c/2*((t-=2)*t*t*t*t + 2) + b; 
    }, 
    easeInSine: function (t, b, c, d) { 
     return -c * Math.cos(t/d * (Math.PI/2)) + c + b; 
    }, 
    easeOutSine: function (t, b, c, d) { 
     return c * Math.sin(t/d * (Math.PI/2)) + b; 
    }, 
    easeInOutSine: function (t, b, c, d) { 
     return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b; 
    }, 
    easeInExpo: function (t, b, c, d) { 
     return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b; 
    }, 
    easeOutExpo: function (t, b, c, d) { 
     return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b; 
    }, 
    easeInOutExpo: function (t, b, c, d) { 
     if (t==0) return b; 
     if (t==d) return b+c; 
     if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b; 
     return c/2 * (-Math.pow(2, -10 * --t) + 2) + b; 
    }, 
    easeInCirc: function (t, b, c, d) { 
     return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b; 
    }, 
    easeOutCirc: function (t, b, c, d) { 
     return c * Math.sqrt(1 - (t=t/d-1)*t) + b; 
    }, 
    easeInOutCirc:function (t, b, c, d) { 
     if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b; 
     return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b; 
    }, 
    //a: amplitude (optional), p: period (optional) 
    easeInElastic: function (t, b, c, d, a, p) { 
     if (t===0) {return b;} 
     if ((t/=d)==1) {return b+c;} 
     if (!p) {p=d*0.3;} 
     var s; 
     if (a < Math.abs(c)) { 
      a=c; 
      s=p/4; 
     }else { 
      a=Math.abs(c); 
      s = p/(2*Math.PI) * Math.asin(c/a); 
     } 
     return -(a*Math.pow(2,10*(t-=1)) * Math.sin((t*d-s)*(2*Math.PI)/p)) + b; 
    }, 
    easeOutElastic: function (t, b, c, d, a, p) { 
     if (t===0) return b; 
     if ((t/=d)==1) return b+c; 
     if (!p) p=d*0.3; 
     var s; 
     if (a < Math.abs(c)) { 
      a=c; 
      s=p/4; 
     }else { 
      a=Math.abs(c); 
      s = p/(2*Math.PI) * Math.asin (c/a); 
     } 
     return a*Math.pow(2,-10*t) * Math.sin((t*d-s)*(2*Math.PI)/p) + c + b; 
    }, 
    easeInOutElastic: function (t, b, c, d, a, p) { 
     if (t===0) return b; 
     if ((t/=d/2)==2) return b+c; 
     if (!p) p=d*(0.3*1.5); 
     var s; 
     if (a < Math.abs(c)) { 
      a=c; 
      s=p/4; 
     }else { 
      a=Math.abs(c); 
      s = p/(2*Math.PI) * Math.asin (c/a); 
     } 
     if (t < 1) { 
      return -0.5*(a*Math.pow(2,10*(t-=1)) * Math.sin((t*d-s)*(2*Math.PI)/p)) + b; 
     } 
     return a*Math.pow(2,-10*(t-=1)) * Math.sin((t*d-s)*(2*Math.PI)/p)*0.5 + c + b; 
    }, 
    easeInBack: function (t, b, c, d, s) { 
     if (s === undefined) s = 1.70158; 
     return c*(t/=d)*t*((s+1)*t - s) + b; 
    }, 
    easeOutBack: function (t, b, c, d, s) { 
     if (s === undefined) s = 1.70158; 
     return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b; 
    }, 
    easeInOutBack: function (t, b, c, d, s) { 
     if (s === undefined) s = 1.70158; 
     if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b; 
     return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b; 
    }, 
    easeInBounce: function (t, b, c, d) { 
     return c - this.easeOutBounce (d-t, 0, c, d) + b; 
    }, 
    easeOutBounce: function (t, b, c, d) { 
     if ((t/=d) < (1/2.75)) { 
      return c*(7.5625*t*t) + b; 
     } else if (t < (2/2.75)) { 
      return c*(7.5625*(t-=(1.5/2.75))*t + 0.75) + b; 
     } else if (t < (2.5/2.75)) { 
      return c*(7.5625*(t-=(2.25/2.75))*t + 0.9375) + b; 
     } else { 
      return c*(7.5625*(t-=(2.625/2.75))*t + 0.984375) + b; 
     } 
    }, 
    easeInOutBounce: function (t, b, c, d) { 
     if (t < d/2) return this.easeInBounce (t*2, 0, c, d) * 0.5 + b; 
     return this.easeOutBounce (t*2-d, 0, c, d) * 0.5 + c*0.5 + b; 
    } 

未經測試的例子:

function animate(element, property, start, end, interval, duration) { 
    var frames = Math.ceil(duration/interval); 
    for(i=0;i<=frames;i++) { 
     (function(f){ 
      setTimeout(function() { 
       element.style[property] = easing_function_from_above(interval*f, start, end, duration); 
      }, interval); 
     })(i); 
    } 
} 
-1

我知道你要的是純淨的JavaScript但如果您嘗試使用jQuery的jQuery的可以處理跨瀏覽器兼容性本身會更好。

你可以嘗試這樣的事情:

$("#"+elem).fadeOut(1000, function() { 
    var img = imgArr[currIndex]; 
    $("#"+elem).attr("src",img); 
    $("#"+elem).fadeIn(1000); 
}); 

它利用其控制的透明度jQuery的淡入淡出和機制。 檢查下面的工作示例。

Image Slider using pure jQuery and without any plugin

0

這是一個很好的問題。它確實幫助我學習了很多東西。事不宜遲,這裏是我想出了:

注意:這是唯一的衰落,這將不與其他風格的動畫幫助

var fade = function (elementInDocument, fadeTo, amountOfTime) { 
    var start = new Date().getTime(), //start with the beginning time 
     getOpacity = function() { 
      //returns the current opacity of the given element 
      var cs = window.getComputedStyle(elementInDocument), 
       co = cs.getPropertyValue('opacity'); 
      return parseFloat(co, 10); //getPropertyValue returns a string, 
             //so you need to float it 
     }, 
     startingOpacity = getOpacity(), //the beginning opacity of the element 
     diff = fadeTo - startingOpacity, //the difference between what you want the opacity to be 
             //and the beginning opacity 
     timer = setInterval(function() { 
      var co = getOpacity(), //start by getting the current opacity (each interval) 
       runTime = new Date().getTime() - start, //the amount of time the function has run 
       progress = runTime/amountOfTime; //the progress of the function 

      if (progress >= 1) { 
       clearInterval(timer); //stop the function because we have completed fade 
      } 
      //takes the beginning opacity and adds it to the product of the difference (diff) 
      //and progress of the function 
      elementInDocument.style.opacity = startingOpacity + (diff * progress); 
     }, 10); 
}; 

//Then to call it 
fade(document.getElementById("someElement"), .6, 3000); 

我試着儘量詳細解釋我可以。

下面是一些測試用例小提琴:http://jsfiddle.net/9U9h8/4/

另外要注意:這是不是跨瀏覽器的,尤其是在舊的(看你的IE < 9)