我想使用鼠标位置垂直滚动图像

时间:2015-02-23 17:55:18

标签: jquery html css

我一直在尝试修改水平滚动图像的代码。我想垂直滚动它。我根本不认识JQuery。经过几个小时的努力,我找不到任何方法来解决这个问题。在这方面,任何人都可以帮助我。

这是整个代码。

$(function() {
  $(window).load(function() {
    var $gal = $("#propertyThumbnails"),
      galW = $gal.outerWidth(true),
      galSW = $gal[0].scrollWidth,
      wDiff = (galSW / galW) - 1, // widths difference ratio
      mPadd = 60, // Mousemove Padding
      damp = 20, // Mousemove response softness
      mX = 0, // Real mouse position
      mX2 = 0, // Modified mouse position
      posX = 0,
      mmAA = galW - (mPadd * 2), // The mousemove available area
      mmAAr = (galW / mmAA); // get available mousemove fidderence ratio
    $gal.mousemove(function(e) {
      mX = e.pageX - $(this).parent().offset().left - this.offsetLeft;
      mX2 = Math.min(Math.max(0, mX - mPadd), mmAA) * mmAAr;
    });
    setInterval(function() {
      posX += (mX2 - posX) / damp; // zeno's paradox equation "catching delay"	
      $gal.scrollLeft(posX * wDiff);
    }, 10);
  });
});
#parent {
  position: relative;
  margin: 0 auto;
  width: 100%;
  background: #ddd;
}
#propertyThumbnails {
  position: relative;
  overflow: hidden;
  background: #444;
  width: 100%;
  white-space: nowrap;
}
#propertyThumbnails img {
  vertical-align: middle;
  display: inline;
  margin-left: -4px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="parent">
  <div id="propertyThumbnails">
    <img src="http://placehold.it/1000x100" />
  </div>
</div>

继承人演示:

http://jsbin.com/alokat/1/edit?html,css,js,output

我将脚本更改为

$(function(){

    $(window).load(function(){

        var $gal   = $("#propertyThumbnails"),
            galW   = $gal.outerHeight(true),
            galSW  = $gal[0].scrollHeight,
            wDiff  = (galSW/galW)-1,  // widths difference ratio
            mPadd  = 60,  // Mousemove Padding
            damp   = 20,  // Mousemove response softness
            mX     = 0,   // Real mouse position
            mX2    = 0,   // Modified mouse position
            posX   = 0,
            mmAA   = galW-(mPadd*2), // The mousemove available area
            mmAAr  = (galW/mmAA);    // get available mousemove fidderence ratio

        $gal.mousemove(function(e) {
            mX = e.pageY - $(this).parent().offset().top - this.offsetTop;
            mX2 = Math.min( Math.max(0, mX-mPadd), mmAA ) * mmAAr;
        });

        setInterval(function(){
            posX += (mX2 - posX) / damp; // zeno's paradox equation "catching delay"    
            $gal.scrollTop(posX*wDiff);
        }, 10);

    });

});

2 个答案:

答案 0 :(得分:1)

您需要定义滚动/溢出的方向。以下是您需要做出的更改:

<强> CSS

#propertyThumbnails {
    position: relative;
    overflow-y: scroll; // or just overflow:auto; should also work
    background: #444;
    width: 100%;
    white-space: nowrap;
}

答案 1 :(得分:1)

您对JavaScript修改很正面,但您还需要稍微更改样式表以适应新行为:

<强> CSS

#propertyThumbnails img {
    display: block;
    /* ... */
}

Working example on JSFiddle.

这一小改动将使浏览器将图像置于下方(block-level elements),而不是 next 彼此(inline elements )。

还要注意JavaScript部分中初始化的改变。