javascript - jQuery 动画 : move image up-down in smaller height div with overflow:hidden

标签 javascript jquery html css

这是 fiddle !

图像应该向上移动直到它的底部边缘到达 div 的底部,然后向下移动直到它的顶部边缘到达父 div 的顶部边缘,以显示它。

这必须适用于不同大小的图像。

$(document).ready(function() {
  move();
});

function move() {
  $(".image").animate({
    bottom: "-=50%"
  }, 10000, 'linear', function() {
    $(".image").animate({
      bottom: "+=50%"
    }, 10000, 'linear', move);
  });
}
.container {
  position: relative;
  width: 1100px;
  height: 480px;
  overflow: hidden;
  background-color: #000;
}

.image {
  position: absolute;
  max-width: 100%;
  min-width: 100%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<div class="container">
  <img class="image" src="http://www.hotel-aramis.com/slider/home/notre-dame-de-paris.jpg />
    </div>

最佳答案

这样的事情似乎可行:

$(document).ready(function () {
  move();
});

function move() {
  $img = $(".image");
  var i = new Image();
  i.src = $img.attr('src');

  var d = i.height - $('.container').height();

  $img.animate({
    bottom: "+="+d+'px'
  }, 10000, 'linear', function () {
    $img.animate({
        bottom: "-="+d+'px'
    }, 10000, 'linear', move);
  });
}

这有点偷偷摸摸,它使用与所选对象相同的 src 创建一个新的 javascript 图像对象(非 Jquery),并使用其自然高度来执行动画。看起来,如果我们创建一个 jQuery 对象,它的高度不一样,也许它需要附加到 dom 或类似的东西。

Jsfiddle:http://jsfiddle.net/VqzvR/8/

将设置代码移到 move() 函数之外意味着 http get(对于 img src)将只执行一次:

$(document).ready(function () {
  setupMove();
});

function setupMove() {
  $img = $(".image");
  var i = new Image();
  i.src = $img.attr('src');

  var d = i.height - $('.container').height();

  function move() {
    $img.animate({
        bottom: "+="+d+'px'
    }, 10000, 'linear', function () {
        $img.animate({
            bottom: "-="+d+'px'
        }, 10000, 'linear', move);
    });
  }
  move();
}

这就是您想要的效果吗?

关于javascript - jQuery 动画 : move image up-down in smaller height div with overflow:hidden,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19621088/

相关文章:

javascript - CKEditor 文本区域内容不显示/不可更新(随机)

javascript - Google 脚本到计算机上的本地项目

javascript - Joomla 中的简单确认弹出窗口

javascript - 如何覆盖 JavaScript 的 Date 对象?

javascript - CSS 不使用 Jquery 添加/删除类进行动画处理

javascript - CSS : round_div is not responsive to its parent div

javascript - 显示从 s3 获取的图像

jquery - 使用 js 获取 <img> 的宽度/高度。没有经过计算的样式

jquery 说 tbody.length = 1 即使没有 <tbody> 标签存在

javascript - CSS:考虑背景颜色的发光效果?