Scroll the page on drag with jQuery

后端 未结 7 1970
无人及你
无人及你 2020-12-01 02:26

I have tried using kinetic.js and the code below, however when I try this in IE11 it keeps jumping to the top every time I scroll:

$(\"html\").kinetic();


        
相关标签:
7条回答
  • 2020-12-01 02:50

    Base on Rory McCrossan's idea, implemented with 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';
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题