div产生的滚动条如何回到顶部

本文介绍如何利用jQuery实现将网页元素和整个页面滚动到顶部的功能,通过编写简单的JavaScript代码,实现页面元素和整体页面的垂直滚动条定位。

        我话少,直接上代码,html代码比较乱,主要为了产生垂直滚动条,看js就Ok了,jquery.js自行下载,就不提供了,so easy!其实说白了就是控制这个div的垂直滚动条的偏移,这里回到顶部,所以就设置成0。顺带着也把页面的垂直滚动条也移到了最顶端。

 

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> New Document </title>
  <meta name="Generator" content="EditPlus">
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <script type="text/javascript" src="jquery.js">
  </script>
	<script type="text/javascript">
	$(function(){
		$('#btn').click(function(){
			$('html,body').animate({scrollTop: '0px'}, 800);
			var container = $('#div1');
			$(container).scrollTop(
				0
			);
		})
	})
	</script>
    
 </head>

 <body>
 <div style="overflow-y:auto; height:500px;">
	  <div  id="div1" style="overflow-y:auto; height:200px;"> <div id="row_8"></div>English is a West Germanic language that was first spoken in early medieval England and is now a global lingua franca.[4][5] It is spoken as a first language by the majority populations of several sovereign states, including the United Kingdom, the United States, Canada, Australia, Ireland, New Zealand and a number of Caribbean nations; and it is an official language of almost 60 sovereign states. It is the third-most-common native language in the world, after Mandarin Chinese and Spanish.[6] It is widely learned as a second language and is an official language of the European Union, many Commonwealth countries and the United Nations, as well as in many world organisations.

English arose in the Anglo-Saxon kingdoms of England and what is now southeast Scotland. Following the extensive influence of Great Britain and the United Kingdom from the 17th to mid-20th centuries through the British Empire, it has been widely propagated around the world.[7][8][9][10] Through the spread of American-dominated media and technology,[11] English has become the leading language of international discourse and the lingua franca in many regions.[12][13]

Historically, English originated from the fusion of closely related dialects, now collectively termed Old English, which were brought to the eastern coast of Great Britain by Germanic settlers (Anglo-Saxons) by the 5th century; the word English is derived from the name of the Angles,[14] and ultimately from their ancestral region of Angeln (in what is now Schleswig-Holstein). The language was also influenced early on by the Old Norse language through Viking invasions in the 9th and 10th centuries.
The Norman conquest of England in the 11th century gave rise to heavy borrowings from Norman French, and vocabulary and spelling conventions began to give the appearance of a close relationship with those of Latin-derived Romance languages (though English is not a Romance language itself)[15][16] to what had then become Middle English. The Great Vowel Shift that began in the south of England in the 15th century is one of the historical events that mark the emergence of Modern English from Middle English.

In addition to words inherited natively from Anglo-Saxon and those borrowed from Norman French, a significant number of English terms are derived from constructions based on root words originally taken from Latin, because Latin in some form was the lingua franca of the Christian Church and of European intellectual life[17] and remains the wellspring of much modern scientific and technical vocabulary.

Owing to the assimilation of words from many other languages throughout history, modern English contains a very large vocabulary, with complex and irregular spelling, particularly of vowels. Modern English has not only assimilated words from other European languages, but from all over the world. The Oxford English Dictionary lists more than 250,000 distinct words, not including many technical, scientific, and slang terms.
</div>
    </div>
	<input id="btn" type="button" value="test">
   <div data-role="header" data-position="fixed" data-fullscreen="true">
		<div data-role="navbar">
			<ul>
				<li><a href="#" data-role="button">首页</a></li>
				<li><a href="#" data-role="button">页面二</a></li>
				<li><a href="#" data-role="button">搜索</a></li>
			</ul>
		</div>
   </div>

   <div data-role="content" style="padding:8px;">
		<ul id="details11">	
			<li>
    			<a href="#" data-role="button">按钮</a>
			</li> 
		</ul>
	</div>
 </body>
</html>

 

在不同场景下,让滚动条回到顶部有多种方法: - **Vue 普通滚动区域**:在 Vue 里,可借助 `ref` 来实现滚动条回到顶部。示例如下: ```vue <template> <div ref="scrollRef"> <!-- 滚动区域 --> </div> </template> <script> export default { methods: { scrollToTop() { this.$refs.scrollRef.scrollTop = 0; } } } </script> ``` 此方法是把 `ref` 绑定到滚动区域的元素上,然后设定 `scrollTop` 属性为 0,就能使滚动条回到顶部 [^1]。 - **Vue 表格滚动区域**:在 Vue 的 `el-table` 组件中,可在 `el-table` 标签添加 `ref`,示例如下: ```vue <template> <el-table ref="tableRef" stripe :data="tableData"> <!-- 滚动区域 --> </el-table> </template> <script> export default { data() { return { tableData: [] }; }, methods: { scrollTableToTop() { this.$refs.tableRef.bodyWrapper.scrollTop = 0; } } } </script> ``` 通过这种方式,能让表格的滚动条回到顶部 [^4]。 - **页面滚动**: - **原生 JavaScript 方式**: ```javascript // 方式一 document.body.scrollTop = document.documentElement.scrollTop = 0; // 方式二 document.body.scrollIntoView(); // 方式三 document.getElementById('site-nav').scrollIntoView(); ``` 第一种方式是直接设置 `document.body` 和 `document.documentElement` 的 `scrollTop` 属性为 0;第二种方式是使用 `scrollIntoView` 方法让 `body` 元素滚动到可见区域顶部;第三种方式是让指定 ID 的元素滚动到可见区域顶部 [^2]。 - **使用 `scrollTo` 方法**: ```javascript document.querySelector('.infinite-scroll-component').scrollTo(0, 0); ``` 该方法能选中想要回到顶部的 DOM 元素,使用 `scrollTo(0, 0)` 使滚动条回到顶部,比如在切换时可保证第一栏始终显示在顶部 [^2]。 - **JS 或 jQuery 方式**: - **直接返回顶部**: ```javascript document.body.scrollTop = 0; ``` - **带有滑动效果(jQuery)**: ```javascript $('#returnTop').click(function() { $('html, body').animate({ scrollTop: 0 }, 'slow'); }); ``` 备注:`returnTop` 是触发返回顶部的元素 ID。这里使用了 jQuery 的 `animate` 方法实现滑动效果 [^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值