Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
大家介紹如何通過(guò) JS/CSS 實(shí)現(xiàn)網(wǎng)頁(yè)返回頂部效果。
CSS 按鈕樣式:
#myBtn {
display: none; /* 默認(rèn)隱藏 */
position: fixed;
bottom: 20px;
right: 30px;
z-index: 99;
border: none;
outline: none;
background-color: red; /* 設(shè)置背景顏色,你可以設(shè)置自己想要的顏色或圖片 */
color: white; /* 文本顏色 */
cursor: pointer;
padding: 15px;
border-radius: 10px; /* 圓角 */
}
var topBtn = document.getElementById('top');
// 獲取視窗高度
var winHeight = document.documentElement.clientHeight;
window.onscroll = function () {
// 獲取頁(yè)面向上滾動(dòng)距離,chrome瀏覽器識(shí)別document.body.scrollTop,而火狐識(shí)別document.documentElement.scrollTop,這里做了兼容處理
var toTop = document.documentElement.scrollTop || document.body.scrollTop;
// 如果滾動(dòng)超過(guò)一屏,返回頂部按鈕出現(xiàn),反之隱藏
if(toTop>=winHeight){
topBtn.style.display = 'block';
}else {
topBtn.style.display = 'none';
}
}
topBtn.onclick=function () {
var timer = setInterval(function () {
var toTop = document.documentElement.scrollTop || document.body.scrollTop;
// 判斷是否到達(dá)頂部,到達(dá)頂部停止?jié)L動(dòng),沒到達(dá)頂部繼續(xù)滾動(dòng)
if(toTop == 0){
clearInterval(timer);
}else {
// 設(shè)置滾動(dòng)速度
var speed = Math.ceil(toTop/5);
// 頁(yè)面向上滾動(dòng)
document.documentElement.scrollTop=document.body.scrollTop=toTop-speed;
}
},50);
}
Spring Boot編程中,我們可以使用多種方式返回HTML頁(yè)面。下面是幾種常用的方法:
Thymeleaf是一款流行的模板引擎,Spring Boot默認(rèn)集成了它。使用Thymeleaf可以方便地生成HTML頁(yè)面,并且支持模板繼承、條件判斷、循環(huán)等常見功能。在Controller中,我們可以將模型數(shù)據(jù)傳遞給Thymeleaf模板,然后渲染生成HTML頁(yè)面。
@GetMapping("/hello")
public String hello(Model model) {
model.addAttribute("name", "world");
return "hello";
}
上面的代碼中,我們將一個(gè)名為"hello"的Thymeleaf模板返回給客戶端,并且傳遞了一個(gè)名為"name"的模型屬性。在模板中,可以使用Thymeleaf的語(yǔ)法進(jìn)行渲染。
優(yōu)點(diǎn):
缺點(diǎn):
Freemarker是另一款常見的模板引擎,它也支持模板繼承、條件判斷、循環(huán)等功能。在Spring Boot中,我們可以使用Freemarker作為模板引擎,生成HTML頁(yè)面。
@GetMapping("/hello")
public String hello(Model model) {
model.addAttribute("name", "world");
return "hello";
}
上面的代碼與Thymeleaf的使用方式類似。只需要將返回值改為模板的名稱即可。
優(yōu)點(diǎn):
缺點(diǎn):
JSP(JavaServer Pages)是一種常見的Java Web頁(yè)面技術(shù),也可以在Spring Boot中使用。在使用JSP時(shí),需要在pom.xml文件中添加對(duì)jsp-api和jstl的依賴,并且需要配置視圖解析器。
@GetMapping("/hello")
public String hello(Model model) {
model.addAttribute("name", "world");
return "hello";
}
上面的代碼中,返回值為"hello",這意味著Spring Boot將查找名為"hello.jsp"的JSP文件,并且將模型數(shù)據(jù)傳遞給它進(jìn)行渲染。
優(yōu)點(diǎn):
缺點(diǎn):
在前后端分離的架構(gòu)中,前端和后端是獨(dú)立的,可以分別開發(fā)和部署。前端使用JavaScript等技術(shù)生成HTML頁(yè)面,后端則提供API接口,返回JSON等數(shù)據(jù)格式。前端通過(guò)調(diào)用后端提供的API接口獲取數(shù)據(jù),并渲染生成HTML頁(yè)面。
@GetMapping("/hello")
public ResponseEntity<Map<String, Object>> hello() {
Map<String, Object> data = new HashMap<>();
data.put("name", "world");
return ResponseEntity.ok(data);
}
上面的代碼中,我們返回一個(gè)Map對(duì)象,包含一個(gè)名為"name"的屬性。在前端中,可以通過(guò)調(diào)用"/hello"接口獲取數(shù)據(jù),并渲染生成HTML頁(yè)面。
優(yōu)點(diǎn):
缺點(diǎn):
在使用Spring Boot返回HTML頁(yè)面時(shí),可能會(huì)出現(xiàn)一些常見問(wèn)題,下面是一些解決方法:
@RequestMapping(value = "/hello", produces = "text/html;charset=UTF-8")
選擇哪種方法返回HTML頁(yè)面取決于具體的需求和項(xiàng)目情況。在選擇方法時(shí),需要考慮開發(fā)成本、渲染速度、易用性等因素,并且需要注意常見問(wèn)題,避免出現(xiàn)不必要的錯(cuò)誤。
*請(qǐng)認(rèn)真填寫需求信息,我們會(huì)在24小時(shí)內(nèi)與您取得聯(lián)系。