【问题标题】:Javascript - Mouse Wheel Go to ClassJavascript - 鼠标滚轮去上课
【发布时间】:2015-08-13 20:41:42
【问题描述】:

每次我使用 鼠标滚轮(你知道正常滚动)我希望 JS(jquery 或其他)滚动到 特定类(或 id 不没关系)。

我有多个 div,所以像 $('body').scrollTo($nextdiv) 这样的代码不是一个选项。

我只想让每个轮子循环移动到具有特定类/ID 的下一个 div。反向滚动也是如此。向上移动一个 div(具有特定类)。

我找到了鼠标滚轮事件以及如何移动到特定的 div,但无法使其协同工作。

动画滚动会很酷。

简单的问题。我可以在同一个 div 中有类和 id 吗?前<div class="a" id="b"> ?

【问题讨论】:

  • 是的,当然你可以有idclass!为什么不?你甚至可以有多个类。
  • 您可以在同一元素中拥有classid。但是,每个id 只能用于一个元素
  • 谢谢。我知道 ID,但还是谢谢。

标签: javascript jquery html mouseevent mousewheel


【解决方案1】:

快速示例,此代码可以改进。最好在 jsfiddle 上进行测试。将鼠标指向列表并滚动。

注意:我没有使用类,但如果您理解我所做的,那么使用类很容易。

注意 2:我只是改变颜色,但逻辑可以替换为任何你想要的。

demo

demo 2 (with classes)

var i = 0;

var list = document.getElementById("list"), length = list.children.length;
list.addEventListener("wheel", ColorLi);

function ColorLi(e) {
    //reset colors
    for(var j = 0; j < length; j++)
        list.children[j].style.color = "black";
    //calculate index
    if(e.wheelDelta > 0)
        i++;
    else
        i--;
    //fix index out of range
    i = i < 0 ? 0 : i;
    i = i > length-1 ? length-1 : i;
    //set color
    list.children[i].style.color = "red";
}
<ul id="list">
    <li style="color: red">A</li>
    <li>B</li>
    <li>C</li>
    <li>D</li>
</ul>

【讨论】:

  • 它不适用于 jsfiddle。我又做错了什么?感谢您的宝贵时间。
  • @user1947700 在字母 A 上移动你的移动并向上或向下移动鼠标滚轮
  • 试过了。不工作。什么都没有发生。我是傻还是怎么的?我点击了您的演示,没有更改任何选项。转到结果面板。试了轮子。什么都没发生。有什么帮助吗?
  • 试过 Chrome。工作正常(只是反转控件)。不适用于 Firefox 40.0
  • @user1947700 你用什么浏览器?另请查看带有类的演示 2。
【解决方案2】:

您可以使用以下插件:jquery.mousewheeljquery.scrollTo 插件,例如:

/*!
 * jQuery Mousewheel 3.1.13
 *
 * Copyright 2015 jQuery Foundation and other contributors
 * Released under the MIT license.
 * http://jquery.org/license
 */
!function(a){"function"==typeof define&&define.amd?define(["jquery"],a):"object"==typeof exports?module.exports=a:a(jQuery)}(function(a){function b(b){var g=b||window.event,h=i.call(arguments,1),j=0,l=0,m=0,n=0,o=0,p=0;if(b=a.event.fix(g),b.type="mousewheel","detail"in g&&(m=-1*g.detail),"wheelDelta"in g&&(m=g.wheelDelta),"wheelDeltaY"in g&&(m=g.wheelDeltaY),"wheelDeltaX"in g&&(l=-1*g.wheelDeltaX),"axis"in g&&g.axis===g.HORIZONTAL_AXIS&&(l=-1*m,m=0),j=0===m?l:m,"deltaY"in g&&(m=-1*g.deltaY,j=m),"deltaX"in g&&(l=g.deltaX,0===m&&(j=-1*l)),0!==m||0!==l){if(1===g.deltaMode){var q=a.data(this,"mousewheel-line-height");j*=q,m*=q,l*=q}else if(2===g.deltaMode){var r=a.data(this,"mousewheel-page-height");j*=r,m*=r,l*=r}if(n=Math.max(Math.abs(m),Math.abs(l)),(!f||f>n)&&(f=n,d(g,n)&&(f/=40)),d(g,n)&&(j/=40,l/=40,m/=40),j=Math[j>=1?"floor":"ceil"](j/f),l=Math[l>=1?"floor":"ceil"](l/f),m=Math[m>=1?"floor":"ceil"](m/f),k.settings.normalizeOffset&&this.getBoundingClientRect){var s=this.getBoundingClientRect();o=b.clientX-s.left,p=b.clientY-s.top}return b.deltaX=l,b.deltaY=m,b.deltaFactor=f,b.offsetX=o,b.offsetY=p,b.deltaMode=0,h.unshift(b,j,l,m),e&&clearTimeout(e),e=setTimeout(c,200),(a.event.dispatch||a.event.handle).apply(this,h)}}function c(){f=null}function d(a,b){return k.settings.adjustOldDeltas&&"mousewheel"===a.type&&b%120===0}var e,f,g=["wheel","mousewheel","DOMMouseScroll","MozMousePixelScroll"],h="onwheel"in document||document.documentMode>=9?["wheel"]:["mousewheel","DomMouseScroll","MozMousePixelScroll"],i=Array.prototype.slice;if(a.event.fixHooks)for(var j=g.length;j;)a.event.fixHooks[g[--j]]=a.event.mouseHooks;var k=a.event.special.mousewheel={version:"3.1.12",setup:function(){if(this.addEventListener)for(var c=h.length;c;)this.addEventListener(h[--c],b,!1);else this.onmousewheel=b;a.data(this,"mousewheel-line-height",k.getLineHeight(this)),a.data(this,"mousewheel-page-height",k.getPageHeight(this))},teardown:function(){if(this.removeEventListener)for(var c=h.length;c;)this.removeEventListener(h[--c],b,!1);else this.onmousewheel=null;a.removeData(this,"mousewheel-line-height"),a.removeData(this,"mousewheel-page-height")},getLineHeight:function(b){var c=a(b),d=c["offsetParent"in a.fn?"offsetParent":"parent"]();return d.length||(d=a("body")),parseInt(d.css("fontSize"),10)||parseInt(c.css("fontSize"),10)||16},getPageHeight:function(b){return a(b).height()},settings:{adjustOldDeltas:!0,normalizeOffset:!0}};a.fn.extend({mousewheel:function(a){return a?this.bind("mousewheel",a):this.trigger("mousewheel")},unmousewheel:function(a){return this.unbind("mousewheel",a)}})});

// The actual code:
$(document).ready(function () {

var targets = $('.scroll'); // List of elements to scroll to
var index = 0;
var duration = 500;
var canScroll = true;
var cache;

function limit(x, min, max) {
	return Math.min(max, Math.max(min, x));
}

$(window).mousewheel(function (ev) {
	
	if (canScroll) {
		
		cache = index;
		
		if (ev.deltaY < 0) {
			index = index + 1; // Scrolling down, so increase index
		} else {
			index = index - 1; // Scrolling up, so decrease index
		}
	
		// Make sure the index is between 0 and (targets.length - 1)
		index = limit(index, 0, targets.length - 1);
		
		console.log(index);
			
		// Make sure to scroll if and only if the value has changed
		if (index !== cache) {
		
		// Scroll to the target element:
		$(window).scrollTo(targets.get(index), {
 			duration: duration,
			easing: 'swing'
		});

		canScroll = false;
		setTimeout(function () {
			canScroll = true;
		}, duration);
	}
}
		
ev.preventDefault();
return false;
});
});
div {
    content: ' ';
    height: 500px;
    background-color: #f2f2f2;
}

div:nth-child(even) {
    background-color: #d0d0d0;
}
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="//cdn.jsdelivr.net/jquery.scrollto/2.1.0/jquery.scrollTo.min.js"></script>

<div class="scroll">div1</div>
<div class="scroll">div2</div>
<div class="scroll">div3</div>
<div class="scroll">div4</div>
<div class="scroll">div5</div>
<div class="scroll">div6</div>

【讨论】:

  • 这适用于任何 div 吗?我想让它向下滚动到多个 div。比如 class="div1" class="div2" class="div3"。因此,每次滚动时,它都会转到具有相同类名的下一个 div。
  • 是的,请参阅编辑。您可以使用模运算符使其重新开始。
  • 这里最好使用ID而不是类:使用#div而不是.div,并在元素上同时使用类和ID。
  • 好的。我会试试看。反向卷轴呢?我想上一格。
  • 那么您将使用--i 而不是++i。或者可能更明显:i -= 1 而不是 i += 1
猜你喜欢
  • 1970-01-01
  • 2014-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-25
  • 1970-01-01
相关资源
最近更新 更多