我试过使用kinetic.js和下面的代码,但是当我在IE11中尝试时,每次滚动时它都会跳到顶部:
$("html").kinetic();
我想让页面在平板电脑和IE10和11上可滚动,这样用户就可以像在移动设备上那样向上推送页面向下滚动。
我如何才能在纯JS或jQuery中做到这一点,而不是跳到顶部呢?
发布于 2014-03-13 09:46:54
我只是想补充一下。使用Rory的代码,我进行了水平滚动。
var clicked = false, base = 0;
$('#someDiv').on({
mousemove: function(e) {
clicked && function(xAxis) {
var _this = $(this);
if(base > xAxis) {
base = xAxis;
_this.css('margin-left', '-=1px');
}
if(base < xAxis) {
base = xAxis;
_this.css('margin-left', '+=1px');
}
}.call($(this), e.pageX);
},
mousedown: function(e) {
clicked = true;
base = e.pageX;
},
mouseup: function(e) {
clicked = false;
base = 0;
}
});
发布于 2015-12-01 21:27:14
此代码将在水平和垂直鼠标拖动滚动上工作。这相当简单。
var curYPos = 0,
curXPos = 0,
curDown = false;
window.addEventListener('mousemove', function(e){
if(curDown === true){
window.scrollTo(document.body.scrollLeft + (curXPos - e.pageX), document.body.scrollTop + (curYPos - e.pageY));
}
});
window.addEventListener('mousedown', function(e){ curDown = true; curYPos = e.pageY; curXPos = e.pageX; });
window.addEventListener('mouseup', function(e){ curDown = false; });
发布于 2017-02-11 08:40:06
基于Rory McCrossan的想法,用AngularJS2实现。
import {Directive, ElementRef, OnDestroy, Input} from "@angular/core";
declare var jQuery: any;
@Directive({
selector: '[appDragScroll]'
})
export class DragScrollDirective implements OnDestroy {
@Input() scrollVertical: boolean = true;
@Input() scrollHorizontal: boolean = true;
private dragging = false;
private originalMousePositionX: number;
private originalMousePositionY: number;
private originalScrollLeft: number;
private originalScrollTop: number;
constructor(private nodeRef: ElementRef) {
let self = this;
jQuery(document).on({
"mousemove": function (e) {
self.dragging && self.updateScrollPos(e);
},
"mousedown": function (e) {
self.originalMousePositionX = e.pageX;
self.originalMousePositionY = e.pageY;
self.originalScrollLeft = jQuery(self.nodeRef.nativeElement).scrollLeft();
self.originalScrollTop = jQuery(self.nodeRef.nativeElement).scrollTop();
self.dragging = true;
},
"mouseup": function (e) {
jQuery('html').css('cursor', 'auto');
self.dragging = false;
}
});
}
ngOnDestroy(): void {
jQuery(document).off("mousemove");
jQuery(document).off("mousedown");
jQuery(document).off("mouseup");
}
private updateScrollPos(e) {
jQuery('html').css('cursor', this.getCursor());
let $el = jQuery(this.nodeRef.nativeElement);
if (this.scrollHorizontal) {
$el.scrollLeft(this.originalScrollLeft + (this.originalMousePositionX - e.pageX));
}
if (this.scrollVertical) {
$el.scrollTop(this.originalScrollTop + (this.originalMousePositionY - e.pageY));
}
}
private getCursor() {
if (this.scrollVertical && this.scrollHorizontal) return 'move';
if (this.scrollVertical) return 'row-resize';
if (this.scrollHorizontal) return 'col-resize';
}
}
https://stackoverflow.com/questions/19743228
复制相似问题