te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Auto scroll div based on mouse position - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Auto scroll div based on mouse position - Stack Overflow

programmeradmin3浏览0评论

I want to automatically scroll a div based on mouse position using jQuery.

If you see this fiddle here, you can see a number of images that are horizontally ordered in a div that is scrollable:

<div id="parent">
    <div id="propertyThumbnails">
        <img src=".jpg" />
        <img src=".jpg" />
        <img src=".jpg" />
        <img src=".jpg" />
        <img src=".jpg" />
    </div>
</div>

CSS:

#parent {
    height: 300px;
    width: 100%;
    background: #ddd;
}
#propertyThumbnails {
    background: #666;
    height: 80px;
    white-space: nowrap;
    overflow: scroll;
}
#propertyThumbnails img {
    width: 125px;
    height: 80px;
    display: inline-block;
    margin: 3px;
    margin-right: 0;
    opacity: 0.6; 
}

I found out that you can use $("#container").scrollLeft(position) to set the position of the scroller but I want to do it based on the mouse position of the parent. So that when the mouse is fully to the right hand side, the right most image displays, and when the mouse is fully left, the left most image displays.

How can I do this?

I want to automatically scroll a div based on mouse position using jQuery.

If you see this fiddle here, you can see a number of images that are horizontally ordered in a div that is scrollable:

<div id="parent">
    <div id="propertyThumbnails">
        <img src="http://www.millport/wp-content/uploads/2013/05/Flower-festival.jpg" />
        <img src="http://www.millport/wp-content/uploads/2013/05/Flower-festival.jpg" />
        <img src="http://www.millport/wp-content/uploads/2013/05/Flower-festival.jpg" />
        <img src="http://www.millport/wp-content/uploads/2013/05/Flower-festival.jpg" />
        <img src="http://www.millport/wp-content/uploads/2013/05/Flower-festival.jpg" />
    </div>
</div>

CSS:

#parent {
    height: 300px;
    width: 100%;
    background: #ddd;
}
#propertyThumbnails {
    background: #666;
    height: 80px;
    white-space: nowrap;
    overflow: scroll;
}
#propertyThumbnails img {
    width: 125px;
    height: 80px;
    display: inline-block;
    margin: 3px;
    margin-right: 0;
    opacity: 0.6; 
}

I found out that you can use $("#container").scrollLeft(position) to set the position of the scroller but I want to do it based on the mouse position of the parent. So that when the mouse is fully to the right hand side, the right most image displays, and when the mouse is fully left, the left most image displays.

How can I do this?

Share Improve this question edited Jun 25, 2013 at 21:56 Roko C. Buljan 206k41 gold badges327 silver badges336 bronze badges asked Jun 25, 2013 at 21:45 ChrisChris 27.4k49 gold badges206 silver badges357 bronze badges 1
  • possible duplicate of How to properly scroll an overflowing div based on mouse position within its container – Rob W Commented Jun 25, 2013 at 21:47
Add a ment  | 

2 Answers 2

Reset to default 10

A slightly different way to achieve what you need:

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).offset().left;
      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: 60%;
  height: 260px;
}

#propertyThumbnails {
  position: relative;
  overflow: hidden;
  background: #444;
  width: 100%;
  height: 262px;
  white-space: nowrap;
}

#propertyThumbnails img {
  vertical-align: middle;
  height: 100%;
  display: inline;
  margin-left: -4px;
}
<div id="parent">
  <div id="propertyThumbnails">

    <img src="//placehold.it/600x400/0bf" />
    <img src="//placehold.it/600x400/f0b" />
    <img src="//placehold.it/600x400/0fb" />
    <img src="//placehold.it/600x400/b0f" />
    <img src="//placehold.it/600x400/bf0" />

  </div>
</div>


<script src="//ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>

where mPadd is the area (in PX, at the left and right border zone) without any sensitivity to prevent user frustrations :)

this should at least get you headed in the right direction.

var parent = $('#parent');
var img = $('img:first-child');

parent.on('mousemove', function(e) {
    mouseX = e.pageX
    img.css('margin-left',-mouseX/parent.width()*100);

});

http://jsfiddle/xWcXt/4/

发布评论

评论列表(0)

  1. 暂无评论