【发布时间】:2019-02-03 16:09:38
【问题描述】:
我有一个图像列表,我在 html 页面中并排显示在一个 div 中。当有很多图片迫使 div 展开时,div 会自动从左到右滚动,反之亦然。
当用户将光标放在那个 div 上时,动画应该停止,当光标消失时,动画应该重新开始。但它不起作用,有时会停止,有时不会。
HTML 由 django 渲染
<div class="row" style="position: relative;">
<div id='mainDiv' class="col-sm-12" style="">
<h1">Title</h1><hr>
<div style="overflow:hidden;white-space: nowrap;">
{% for img in all_images %}
<div class="" style="height:200px;display:inline-block">
<img height="100" title="" src="{{img.scr}}">
</div>
{% endfor %}
</div>
</div>
</div>
JS
function animatethis(targetElement, speed) {
var scrollWidth = $(targetElement).get(0).scrollWidth;
var clientWidth = $(targetElement).get(0).clientWidth;
$(targetElement).animate({ scrollLeft: scrollWidth - clientWidth },
{
duration: speed,
complete: function () {
targetElement.animate({ scrollLeft: 0 },
{
duration: speed,
complete: function () {
animatethis(targetElement, speed);
}
});
}
});
};
var sec = 20000;
animatethis($('#mainDiv'), sec);
$("#mainDiv").hover(function(){
$(this).stop(true)
});
$("#mainDiv").mouseout(function(){
animatethis($(this), sec);
});
【问题讨论】:
标签: javascript jquery html css django