How can you replace e.path [4] in this function to make the code cross-browser?

  • 0
    let scrollBtn = {
    	start: function(){
    		let that = this;
    		document.addEventListener('click', function(e){
    			that.scroll(e)
    		})
    	}, 
    
    	scroll: function(e){
    		if(e.target.classList.contains('bottom_scroll')){
    			let parentElement = e.path[4];
    			let elementPlace = parentElement.getClientRects();
    			let coordinateY = elementPlace[0].y;
    			let elementHeight = elementPlace[0].height;
    			let curentScroll = window.pageYOffset; 
    			let scrollDistance = curentScroll + elementHeight + coordinateY;
    			let timer = setInterval(scroll, 1);
    			function scroll() {
    				window.scrollTo(0, curentScroll);
    				curentScroll += 5;
    				if(curentScroll > scrollDistance -5){
    					window.scrollTo(0, scrollDistance);
    					clearInterval(timer);
    				}
    			}
    		} else if(e.target.classList.contains('top_scroll')){
    			let timer = setInterval(scroll, 1);
    			let curentScroll = window.pageYOffset; 
    			function scroll() {
    				window.scrollTo(0, curentScroll);
    				curentScroll -= 20;
    				if(curentScroll < 20){
    					clearInterval(timer);
    					window.scrollTo(0, 0);
    				}
    			}
    		} else {
    			return false;
    		}
    	}
    }
    scrollBtn.start()
    JavaScript Isaac Klein, Feb 5, 2020

  • 0 Answers
Your Answer
To place the code, please use CodePen or similar tool. Thanks you!