【问题标题】:Horizontal scroll on mouseMove - wide div in smaller div with overflow:hidden (Can't get the math to work)mouseMove 上的水平滚动 - 较小 div 中的宽 div 溢出:隐藏(无法让数学工作)
【发布时间】:2013-04-17 02:46:12
【问题描述】:

我正在尝试制作一个图像拇指“线”,它会在鼠标移动时滚动。我让它工作了,但我现在的问题是我想在两边做一个“填充”,所以我不必把鼠标一直放在两边就能看到第一个/最后一个拇指。但我真的无法让它工作:/

这是我现在的脚本:

// MouseMove scrolling on thumbs
var box = $('.thumbs-block'),
    innerBox = $('.thumbs'),
    lastElement = innerBox.find('a:last-child');

var offsetPx = 100;
var boxOffset = box.offset().left;

var boxWidth = box.width() /* - (offsetPx*2)*/;
var innerBoxWidth = (lastElement[0].offsetLeft + lastElement.outerWidth(true)) - boxOffset /* + (offsetPx*2)*/;

scrollDelayTimer = null;
box.mousemove(function (e) {
    console.log('boxWidth: ' + boxWidth + '   innerBoxWidth: ' + innerBoxWidth + '   box.scrollLeft(): ' + box.scrollLeft());

    var mouseX = e.pageX;
    var boxMouseX = mouseX - boxOffset;

    if ((boxMouseX > offsetPx) && (boxMouseX < (boxWidth - offsetPx))) {
        var left = (boxMouseX * (innerBoxWidth - boxWidth) / boxWidth) /* - offsetPx*/;

        clearTimeout(scrollDelayTimer);
        scrollDelayTimer = setTimeout(function () {
            scrollDelayTimer = null;
            box.stop().animate({
                "scrollLeft": left
            }, {
                queue: false,
                duration: 500,
                easing: 'linear'
            });
        }, 10);
    }
});

我尝试在一些地方添加偏移量(内联注释掉),其中一些让它在一端工作,但不是在另一端工作:/

我很确定这是数学问题,但我不知道该怎么办:/

这是一个 jsFiddle:http://jsfiddle.net/6CJfs/1/

我希望我的问题足够清楚,不知道如何解释,希望有人能提供帮助:)

【问题讨论】:

  • 只是在我脑海中你能做的就是以某种方式告诉它内框小于它的大小,然后相应地将最大值添加到 scrolleft

标签: jquery math scroll mousemove


【解决方案1】:

你的脚本不流畅,所以我完全修改了它(对不起:)
用一个非常简单的方法:

$(function() {

  const $bl = $(".thumbs-block"),
    $th = $(".thumbs"),
    blW = $bl.outerWidth(),
    blSW = $bl.prop("scrollWidth"),
    wDiff = (blSW / blW) - 1, // widths difference ratio
    mPadd = 60, // Mousemove Padding
    damp = 20; // Mousemove response softness

  let posX = 0,
    mX2 = 0, // Modified mouse position
    mmAA = blW - (mPadd * 2), // The mousemove available area
    mmAAr = (blW / mmAA), // get available mousemove fidderence ratio
    itv = null;

  const anim = () => {
    posX += (mX2 - posX) / damp; // zeno's paradox equation "catching delay"    
    $th.css({
      transform: `translateX(${-posX * wDiff}px)`
    });
  };

  $bl.on("mousemove", function(e) {
    const mouseX = e.pageX - $(this).prop("offsetLeft");
    mX2 = Math.min(Math.max(0, mouseX - mPadd), mmAA) * mmAAr;
  }).on("mouseenter", function(e) {
    itv = setInterval(anim, 10);
  }).on("mouseleave", function() {
    clearInterval(itv);
  });

});
.thumbs-block {
  position: relative;
  overflow: hidden;
  max-width: 100%;
}

.thumbs-block .thumbs {
  display: flex;
  flex-flow: row nowrap;
}
<div class="thumbs-block">
  <div class="thumbs">
    <a class="thumb"><img src="http://placehold.it/120x120/0bf&text=01" /></a>
    <a class="thumb"><img src="http://placehold.it/120x120/f0b&text=02" /></a>
    <a class="thumb"><img src="http://placehold.it/120x120/bf0&text=03" /></a>
    <a class="thumb"><img src="http://placehold.it/120x120/b0f&text=04" /></a>
    <a class="thumb"><img src="http://placehold.it/120x120/fb0&text=05" /></a>
    <a class="thumb"><img src="http://placehold.it/120x120/0fb&text=06" /></a>
    <a class="thumb"><img src="http://placehold.it/120x120/0bf&text=07" /></a>
    <a class="thumb"><img src="http://placehold.it/120x120/f0b&text=08" /></a>
    <a class="thumb"><img src="http://placehold.it/120x120/bf0&text=09" /></a>
    <a class="thumb"><img src="http://placehold.it/120x120/b0f&text=10" /></a>
    <a class="thumb"><img src="http://placehold.it/120x120/fb0&text=11" /></a>
    <a class="thumb"><img src="http://placehold.it/120x120/0fb&text=12" /></a>
  </div>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

【讨论】:

  • 抱歉,回答晚了,一直很忙;)但它看起来真的很棒:P 非常感谢您的帮助,希望我能在今天晚些时候在我的项目中实现它;)
  • 我刚刚在我的项目中测试了它,就像一个魅力:P 感谢您的帮助,投票并接受了;)
  • 需要帮助。它在调整窗口大小时没有得到适当的宽度,因此一些第一个和最后一个 div 被隐藏了。有什么办法解决吗?
  • 能否更改此脚本以与同一页面中的各种轮播一起使用?如果有,怎么做?
  • @user1991185 您可以将其包装在 .each() 中,但我建议创建一个灵活且可重用的 jQuery 插件:learn.jquery.com/plugins/basic-plugin-creation ...有很大帮助,我目前没有时间创建一个 sn-p。
【解决方案2】:
var $imgWrapper = $(".js-img-wrapper");
var $imgInnerWrapper;
var elementLength = 0;
var width = 0;
var elementRangeWidth = 0;
var saveShowedNumberOfElement = 1;

$imgWrapper.on("mouseenter", function() {
    $imgInnerWrapper = $(this).find(".js-img-inner");
    elementLength = $imgInnerWrapper.children().length;
    width = $(this).outerWidth();
    elementRangeWidth = width / elementLength;
}).on("mousemove", function(e) {
    var mousePosition = e.pageX - $(this).offset().left;
    if (mousePosition > 0 && mousePosition <= width) {
        var showedNumberOfElement = Math.ceil(mousePosition / elementRangeWidth);

        if (saveShowedNumberOfElement !== showedNumberOfElement) {
            saveShowedNumberOfElement = showedNumberOfElement;
            var imagePositionOffset = -width * (showedNumberOfElement - 1);
            $imgInnerWrapper.css({
                transform: "translateX("+ imagePositionOffset +"px)"
            });
        }
    }
}).on("mouseleave", function() {
    $imgInnerWrapper.css({
        transform: "translateX(0px)"
    });

    $imgInnerWrapper = undefined;
    elementLength = 0;
    width = 0;
    elementRangeWidth = 0;
    saveShowedNumberOfElement = 1;
});

您可以在 CodePen 上观看它的工作原理

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-10
    • 2017-11-06
    • 2013-11-25
    • 1970-01-01
    • 1970-01-01
    • 2021-01-27
    • 1970-01-01
    相关资源
    最近更新 更多