我有一个名为#mousearea的包装器,我有一个名为#mouseshift的div,当我将鼠标悬停在#mousearea上时,我想在特定范围内移动translate3d(0,230%,0)值。

我已经完成了mousemove的工作,但是我现在最终得到了像translate3d(7881%,230%,0)这样的东西,它太敏感了,我希望它在诸如0-60%之类的东西之间转换X坐标,所以它要微妙得多。

这是我到目前为止的内容:

jQuery(document).ready(function($){
    $('#mousearea').mousemove(function (e) {

        var shiftAmount = 1;

        $('#mouseshift').css(
            'transform', 'rotate(90deg) translate3d(' + -e.pageY + shiftAmount + '%,230%,0)'
        );
    });
});

更新:

这更接近一点,除了它会记录正确的translate3d,但不会将其应用于#mouseshift。
$('#mousearea').mousemove(function(e){
        var x = e.pageY - this.offsetTop;
        var transfromPosition = 'translate3d(' + x + ', 230%, 0)';
        console.log(transfromPosition);

        if ((x <= 800)) {
            //$('#mouseshift').css({'top': x});
            $('#mouseshift').css('transform', transfromPosition);
        }
    });

最终解决方案:
jQuery(document).ready(function($){

    $('#mousearea').mousemove(function(e){

        var min = 50;
        var max = 70;

        var x = e.pageY;
        var windowHeight = window.innerHeight;

        scrolled = (x / windowHeight);
        percentageScrolled = scrolled * 100;

        offsetScroll = max - min;
        offsetPercentage = scrolled * 20;
        translateX = min + offsetPercentage;


        console.log(x + 'px');
        console.log(windowHeight + 'px window height');
        console.log(percentageScrolled + '% scrolled');
        console.log(offsetScroll + 'offset scroll');
        console.log(offsetPercentage + '% offset percentage');




        var transfromPosition = 'rotate(90deg) translate3d(' + translateX + '%, 230%, 0)';
        $('#mouseshift h1').css('transform', transfromPosition);
    });

});

转换为可重用的插件,我现在想扩展它以与多个对象一起工作,并且每个对象将具有不同的max和min值:

这就是我所拥有的,但似乎只影响元素max和min上的所有项目。
$(function () {
    $('#mouseshift-1, #mouseshift-2').mouseShift();
});
(function ($) {
    $.fn.mouseShift = function () {
        return this.each(function () {
            var myEl = $(this);
            var min = $(this).data('min');
            var max = $(this).data('max');

            $('#mousearea').mousemove(function (e) {
                var yPosition = e.pageY;
                var windowHeight = window.innerHeight;

                scrolled = (yPosition / windowHeight);
                //percentageScrolled = scrolled * 100;
                offsetRange = max - min;
                offsetRangePercentage = scrolled * 20;
                offset = min + offsetRangePercentage;

                ////  Debug
                console.log('max: ' + max + ', Min:' + min);
                console.log(yPosition + 'px');
                console.log(windowHeight + 'px window height');
                //console.log(percentageScrolled + '% scrolled');
                console.log(offsetRange + 'px offset scroll');
                console.log(offsetRangePercentage + '% offset percentage');

                var transfromPosition = 'rotate(90deg) translate3d(' + offset + '%, 230%, 0)';
                myEl.css('transform', transfromPosition);
            });
        });
    };
})(jQuery);

还有一些HTML可以让您更加清楚:
<div class="column"><h1 id="mouseshift-1" data-min="50" data-max="70">boo</h1></div>
      <div class="column"><h1 id="mouseshift-2" data-min="20" data-max="90">bah</h1></div>
      <div class="column"><h1 id="mouseshift-3" data-min="80" data-max="100">bing</h1></div>

最佳答案

我认为您正在寻找的是可以分配的平均值。最好的方法是除以它可以移动的最大数量,再乘以它可以具有的最大值,因此基本上:

position / maxposition * maxvalue

第一位将返回01之间的数字,而最后一位将使其成为060之间的值。下面,我构建了一个简单的(无jquery的)版本,以显示其工作方式:

var mousePointer = document.getElementById('test')

document.addEventListener('mousemove', function(e){
  var x = e.pageX / window.innerHeight;
      x = x * -60;
  mousePointer.style.webkitTransform = 'translateX(' + x + '%)';
  mousePointer.style.transform = 'translateX(' + x + '%)';
})
#test {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 20px;
  height: 20px;
  background: red;
}
<div id="test"></div>


更新:可重用代码段

我不太喜欢使用jQuery,因此它将再次使用普通javascript(但非常简单)。那就是您想要的-可重用插件吗?

var divs = Array.prototype.slice.call(document.querySelectorAll('[data-range]'));

document.addEventListener('mousemove', function(e){

  var eased = e.pageX / window.innerWidth;

  divs.forEach(function(div){

    var range = div.getAttribute('data-range').split(',');
    var min = parseFloat(range[0]);
    var max = parseFloat(range[1]);

    var ease = min + (eased * (max - min));

    div.style.webkitTransform = 'translateX(' + ease + '%)';
    div.style.transform = 'translateX(' + ease + '%)';

  });

});
div {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 200px;
  height: 200px;
  background: gray;
}

#d2 { background: yellow; }
#d3 { background: #666; }
<div data-range="60,70" id="d1"></div>
<div data-range="-70,70" id="d2"></div>
<div data-range="-60,-70" id="d3"></div>

10-08 08:34