h5新增 history的应用
生活随笔
收集整理的這篇文章主要介紹了
h5新增 history的应用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
(1)history api 的基本介紹:
history.back():上一條 history.forward():下一條 history.go():相對于當前頁面的前進或后退
新增加的api: history.pushState() :追加歷史
history.replaceState(); 替換歷史
上面兩個函數都有3個參數 (1.存數據 null 2.標題 null 記錄的地址)
window.onpopstate=function(){
監聽歷史切換事件.
}
單頁面應用程序:SPA (single page application)
實現方案:(1) 哈希 hash (2)歷史追加 特點:改變地址欄是不會跳轉的.
ajax 渲染頁面 優點:提高用戶體驗 缺點:不利于seo
解決方案:ajax渲染頁面的同時,更改地址欄(地址欄在服務器端一定要有相對應的頁面)
(2)一個 SPA的小案例:
.template.html代碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>history</title>
<style>
*{
padding: 0;
margin: 0;
}
body {
background-color: #F7F7F7;
font-family: Arial;
}
header {
background: #242424;
border-bottom: 1px solid #000;
}
.wrapper{
width: 1100px;
height: 70px;
margin: 0 auto;
}
header .wrapper h1{
float: left;
width: 176px;
height: 69px;
background: url(images/topbar.png) no-repeat 0 0;
font-size: 0;
}
header .wrapper ul{
list-style:none;
}
header .wrapper ul li{
float: left;
height: 70px;
}
header .wrapper ul li.now,
header .wrapper ul li:hover{
background: #12b7f5;
}
header .wrapper ul li a{
padding: 0 20px;
display: block;
color: #fff;
line-height: 70px;
text-decoration: none;
}
.content{
width: 1100px;
margin: 0 auto;
font-size: 100px;
text-align: center;
}
</style>
</head>
<body>
<header>
<div class="wrapper">
<h1>網易云音樂</h1>
<ul>
<li data-page="index" class="<?php echo $page=='index'?'now':'' ?>" ><a href="index.php">發現音樂</a></li>
<li data-page="my" class="<?php echo $page=='my'?'now':'' ?>"><a href="my.php">我的音樂</a></li>
<li data-page="friend" class="<?php echo $page=='friend'?'now':'' ?>"><a href="friend.php">朋友</a></li>
</ul>
</div>
</header>
<div class="content">
<?php echo $html ?>
</div>
<script src="js/jquery.min.js"></script>
<script src="js/index.js"></script>
</body>
</html>
?
? ?.index.js代碼:
/*1.ajax異步加載 渲染頁面*//*2.渲染什么頁面需要和后臺提供的地址保持一致*/
/*3.切換歷史渲染頁面*/
$(function () {
$('.wrapper').on('click','a',function () {
//渲染頁面 頁面標識
var page = $(this).parent().data('page');
render(page);
/*地址保持一致*/
/*追加地址 而且這個地址后臺一定要存在*/
var historyUrl = $(this).attr('href');
history.pushState(null,null,historyUrl);
return false;
});
/*監聽切換歷史*/
window.onpopstate = function () {
/*獲取地址欄信息*/
console.log(location.pathname);
/*根據信息判斷需要加載什么頁面的內容*/
var pathname = location.pathname;
var page = 'index';
if(pathname.indexOf('index.php') > -1 ){
page = 'index';
}else if(pathname.indexOf('my.php') > -1){
page = 'my'
}else if(pathname.indexOf('friend.php') > -1){
page = 'friend';
}
render(page);
}
});
var render = function (page) {
/*怎么調用ajax 請求方式 請求地址 請求參數 返回數據結構和意義 */
/*發出請求*/
$.ajax({
type:'get',
url:'api/data.php',
data:{
page:page
},
dataType:'json',
success:function (data) {
/*渲染頁面*/
/*選中樣式*/
$('[data-page]').removeClass('now');
/*data返回了當前頁面的標識*/
$('[data-page="'+data.page+'"]').addClass('now');
/*網頁內容*/
$('.content').html(data.html);
}
});
}
效果圖:
?
轉載于:https://www.cnblogs.com/buautifulgirl/p/9741840.html
總結
以上是生活随笔為你收集整理的h5新增 history的应用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: scanperiod 不生效
- 下一篇: 思维风暴 codeforces (106