您当前的位置: 首页 >  jquery

梁同学与Android

暂无认证

  • 5浏览

    0关注

    618博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

jQuery兼容IE和Chrom浏览器-得到页面滚动条的坐标值

梁同学与Android 发布时间:2020-02-29 14:21:03 ,浏览量:5

我们先来看一下代码:


  
    
    My JSP 'practice_02.jsp' starting page
	
	
	    
	
	
	
	
		.all {
			width:100%;
			height:120%;
		}
		.scroll {
			width:200px;
			height:100px;
			overflow: scroll;
		}
	
  
  
  	
	 	
		  	this is niuniu love
		  	this is niuniu love
		  	this is niuniu love
		  	this is niuniu love
		  	this is niuniu love
		  	this is niuniu love
		  	this is niuniu love
		  	this is niuniu love
	 	
	 	
	 	得到scrollTop的坐标
	 	设置scrollTop的坐标
  	
  	
	
		$('#btn1').click(function() {
			console.log('以下是滚动条的位置');
			console.log($('html').scrollTop());
		});
	
  

当我们使用Chrome浏览器滚动浏览器的滚动条时发现是每次取的值都是0,如下:

没有滚动的时候 在这里插入图片描述 滚动之后 在这里插入图片描述

但是在IE浏览器就可以取到值 在这里插入图片描述

所以我们将代码做一下修改,修改部分如下:


		$('#btn1').click(function() {
			console.log('以下是滚动条的位置');
			console.log($('body').scrollTop());
		});

我们发现Chrome浏览器可以取到值了 在这里插入图片描述 经过小编测试部分版本的IE浏览器的值为0

所以我们看最终的代码:


  
    
    
    My JSP 'practice_02.jsp' starting page
    
	
	
	    
	
	
	
	
		.all {
			width:100%;
			height:120%;
		}
		.scroll {
			width:200px;
			height:100px;
			overflow: scroll;
			
		}
	
  
  
  	
	 	
		  	this is niuniu love
		  	this is niuniu love
		  	this is niuniu love
		  	this is niuniu love
		  	this is niuniu love
		  	this is niuniu love
		  	this is niuniu love
		  	this is niuniu love
	 	
	 	
	 	得到scrollTop的坐标
	 	设置scrollTop的坐标
  	
  	
	
		$('#btn1').click(function() {
			console.log('以下是滚动条的位置');
			console.log($('body').scrollTop()+$('html').scrollTop());
		});
	
  

关注
打赏
1660730345
查看更多评论
立即登录/注册

微信扫码登录

0.0486s