我試圖創建一個視頻預覽時,用戶懸停的圖像(如youtube)。jQuery視頻幀預覽圖像懸停
這些圖像取自FFmpeg,每100幀獲取一幀,並保存在第一個加載圖像的相同目錄中,例如:20-1.jpg
。 當用戶將鼠標在第一圖像上,它應該通過名爲例如其他圖像週期:20-2.jpg
,20-3.jpg
,20,4.jpg
等
HTML
獲取項目的ID從Twig
{{result.id}}
<div class="card result-card" onmouseover="updateThumb({{ result.id }})">
<a href="{{ url('vedi_oggetto', {'category': catName,'item': result.id,'title': slug}) }}">
<img class="card-img-top img-fluid" id="imgPreview-{{ result.id }}" src="/uploads/photos/{{ result.id }}-1.jpg" alt="Card image cap">
</a>
</div>
JS
function updateThumb(id) {
var timer,
count = 1,
cycle = function(el){
var s = el.attr('src'),
root = s.substring(0, s.lastIndexOf('/') + 1);
count = (count+1)%5;
el.attr('src', root + '2-' + ((count===0) ? '5' : count) + '.jpg');
};
$('#imgPreview-' + id).hover(function(){
var $this = $(this);
cycle($this);
timer = setInterval(function(){ cycle($this); }, 800);
}, function(){
clearInterval(timer);
});
}
問題是,當我將鼠標懸停在第一張圖片上時,它按預期方式工作並循環。但是,當我將鼠標懸停在第二張圖像或其他圖像上時,它會顯示所有圖像的第一個圖像週期,因此它僅適用於一張圖像。
在每個'mouseover'再指定'$(「#imgPreview-」 + id).hover(function(){..})'事件,這是錯誤的伴侶:) – skobaljic
此外,最好使用'mouseenter'和'mouseleave'事件,因爲...你可能會失去圖像懸停...並在替換圖像的'src'屬性之前嘗試預先加載圖像。祝你好運 – skobaljic
夥計們,感謝您的想法,可以在回覆中發佈示例嗎? – andreaem