【问题标题】:How to scroll to the bottom of an image on hover如何在悬停时滚动到图像底部
【发布时间】:2017-10-10 15:43:45
【问题描述】:

您好,我很想知道在悬停时滚动到图像底部的最佳方法。该图像位于一个包装器中,溢出隐藏应用到它很长。理想情况下,平滑滚动到底部是我要查看隐藏图像的其余部分的内容。

我一直在玩 jquery 试图让它工作,但我陷入了混乱。不确定我的代码是否在正确的轨道上。

感谢任何帮助

$('#image-wrap img').on('hover', function() {
  var target = $(this),
    height = target.height(),
    scrollHeight = target.prop('scrollHeight');

  target.animate({
    scrollTop: scrollHeight - height
  }, 2000);
});
#main-wrapper {
  width: 600px;
  margin: 0 auto;
}

#box-wrapper {
  position: relative;
}

#box {
  width: 100%;
  height: 400px;
  border: 1px black solid;
  position: relative;
}

#image-wrap {
  overflow: hidden;
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
}

#image-wrap img {
  position: absolute;
  top: 0;
  width: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="main-wrapper">
  <div id="box-wrapper">
    <div id="box">
      <div id="image-wrap">
        <img src="http://via.placeholder.com/600x2000" />
      </div>
    </div>
  </div>
</div>

【问题讨论】:

    标签: jquery html css image


    【解决方案1】:

    您可以简单地使用 CSS3。在悬停时添加过渡transform: translateY(-100%),但它不像 jquery 解决方案那样聪明。

    #main-wrapper {
      width: 600px;
      margin: 0 auto;
    }
    
    #box-wrapper {
      position: relative;
    }
    
    #box {
      width: 100%;
      height: 400px;
      border: 1px black solid;
      overflow: hidden;
    }
    
    img {
        transition: all 2000ms ease-out;
    }
    
    #box:hover img {
        transform: translateY(-100%);
    }
    <div id="main-wrapper">
      <div id="box-wrapper">
        <div id="box">
            <img src="http://lorempixel.com/400/1000/sports/" />
        </div>
      </div>
    </div>

    【讨论】:

      【解决方案2】:

      我现在明白你想要的样子了。

      HTML

      $(document).ready(function(){
        $('#img-holder').mouseenter(function(){
          var x = $('img').height();
          x = x-400;
          $('img').animate({'position':'absolute','top':'-'+x}, 300);
        });
         $('#img-holder').mouseleave(function(){
           $('img').css({'position':'relative','top':'0'});
         });
      });
      #img-holder{
        width:100%;
        height:400px;
        border:2px solid;
        overflow:hidden;
        position:relative;
      }
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <div id="img-holder">
        <img src="https://blog.hittail.com/wp-content/uploads/sites/8/2013/01/hidden-value-of-long-tail-seo-1000.png" width="100%">
      </div>

      这个想法是让img的位置绝对,并给它一个值来显示底部。

      这是一个有效的DEMO

      【讨论】:

      • 这是正确答案。我将代码编辑为有效的 sn-p。
      • 感谢您的努力 =)
      【解决方案3】:

      请尝试以下方法:

      $('#image-wrap img').hover(function() {
        $('html, body').animate({
              scrollTop: $(this).offset().top + $(this).outerHeight()
          }, 2000);
      });
      #main-wrapper {
        width: 600px;
        margin: 0 auto;
      }
      
      #box-wrapper {
        position: relative;
      }
      
      #box {
        width: 100%;
        height: 400px;
        border: 1px black solid;
        position: relative;
      }
      
      #image-wrap {
        overflow: hidden;
        position: absolute;
        width: 100%;
        height: 100%;
        top: 0;
      }
      
      #image-wrap img {
        position: absolute;
        top: 0;
        width: 100%;
      }
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <div id="main-wrapper">
        <div id="box-wrapper">
          <div id="box">
            <div id="image-wrap">
              <img src="http://via.placeholder.com/600x2000" />
            </div>
          </div>
        </div>
      </div>

      【讨论】:

      • 运行 sn-p 的绝佳 +1
      • 伙伴你不明白当包装 div 上隐藏溢出时你不能做滚动功能。 “图像在一个包装中,溢出隐藏应用到它很长。” cr8code.co/editor.php?workid=79e9c857b463e73b0f491b771a446e79这就是答案。
      • @GüneySaramalı,你是对的。 OP的问题是模棱两可的。如果您想滚动到隐藏溢出的 div 中图像的底部,那是不可能的。如果您想将页面滚动到包装器 div 本身的底部,那是可以的,因为外部容器已经溢出。
      • 不,他说的不是模棱两可,“图像在包装中,溢出隐藏应用到它很长。”我不知道问题是否已编辑,但当我读到此内容时已说明
      • 好吧,我想你是对的。也许解决方案是尝试将溢出隐藏到外部 div。
      【解决方案4】:

      如果图像被隐藏,您将无法与之交互。如果它是可见的,你可以像这样使用scrollTo()。

      1. 使用 scrollTop() 捕获当前滚动位置

        var pos = $('你的图片容器').scrollTop();

      2. 抓取图片容器的高度

        var height = $('你的图片容器').height();

      3. 将两个值相加

        var moveto = pos + height;

      4. 自动滚动到该位置

        window.scrollTo(0, moveto);

      【讨论】:

        【解决方案5】:

        所以我猜你想滚动整个页面(有点像使用鼠标滚轮)?

        你可以这样做:

        $('#image-wrap img').on('hover', function() {
          var target = $(this),
            height = target.height(),
            scrollHeight = target.offset().top;
        
            $('body,html').animate({
                 scrollTop:scrollHeight+height-500
            },500);
        });
        

        这将滚动页面,因此图像的最后 500 像素可见。如果这不能解决您的问题,整个页面的示例会很有帮助。

        【讨论】:

        猜你喜欢
        • 2018-05-30
        • 2018-01-28
        • 1970-01-01
        • 2014-12-12
        • 1970-01-01
        • 1970-01-01
        • 2022-10-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多