SPA页面初试

之前一直很好奇,SPA应用到底是怎么实现的,昨天无意间看到了有一篇介绍的文章,就想着来试一下水(以下根据我的理解所写,可能会让你看的云里雾里,如果想加深了解,最好先了解下window.location.hash是什么东西)

DEMO:

<!DOCTYPE html>
<html>

	<head>
		<title>SPA</title>
		<style type="text/css">
			* {
				padding: 0px;
				margin: 0px;
			}
			
			html,
			body {
				height: 100%;
				width: 100%;
			}
			
			.pageview {
				width: 100%;
				height: 100%;
			}
		</style>
		<script type="text/javascript" src="js/jquery-3.1.1.js"></script>
		<script type="text/javascript">
			var states;
			var currentState;
			$(document).ready(function() {
					states = registState();
					currentState = init();
					//监听hash路由变化
					window.addEventListener("hashchange", function() {
						var nextState;
						console.log(window.location.hash);
						//判断地址是否为空,若为空,则默认到main-view页面
						if(window.location.hash == "") {
							nextState = "main-view";
						} else {
							//若不为空,则获取hash路由信息,得到下一个状态
							nextState = window.location.hash.substring(1);
						}
						//判断当前状态是否注册过(是有存在这个状态)0g
						var validState = checkState(states, nextState);
						//若不存在,则返回当前状态
						if(!validState) {
							console.log("you enter the false validState");
							window.location.hash = "#" + currentState;
							return;
						}
						$('#' + currentState).css("display", "none");
						$('#' + nextState).css("display", "block");
						currentState = nextState;
					})

				})
				//状态注册
			function registState() {
				var states = [];
				//状态注册
				$(".pageview").map(function() {
					return states.push($(this)[0].id);
				})
				return states;
			}
			//初始化,对用户一开始输入的url进行处理
			function init() {
				var currentState = window.location.hash.substring(1);
				if(currentState == "") {
					currentState = "main-view";
				}
				if(currentState != "main-view") {
					$('#main-view').css("display", "none");
					$('#' + currentState).css("display", "block");
				}
				return currentState;
			}
			//判断状态是否存在
			function checkState(states, nextState) {
				var tof = false;
				states.forEach(function(element) {
					if(element == nextState) {
						tof = true;
					}
				})
				return tof;
			}
		</script>
	</head>

	<body>
		<div class="pageview" style="background: #3b76c0" id="main-view">
			<h3>首页</h3>
			<div title="-list-view" class="right-arrow"></div>
		</div>
		<div class="pageview" style="background: #58c03b;display: none" id="list-view">
			<h3>列表页面</h3>
			<div class="left-arrow"></div>
			<div title="-detail-view" class="right-arrow"></div>
		</div>
		<div class="pageview" style="background: #c03b25;display: none" id="detail-view">
			<h3>列表详情页面</h3>
			<div class="left-arrow"></div>
		</div>
	</body>

</html>

  

其实,SPA的原理就是,一开始将一些必要的页面都加载进来,当你在页面输入别的路由的时候,其实还是待在当前的页面,只不过是他识别出你想要去的地址,然后将那个页面的内容获取到,替代掉当前页面的内容,并且相应的改变url地址,这样给人看起来就好像到了另一个页面,实际上你还是在这个页面里,没有离开过.
比如,例如当前你在localhost:8080/index.html这个页面时,你想跳转到#list-view页面(使用hashChange),或者你点击某个跳转按钮要跳转到那个页面的时候,他先获取你那个#list-view页面的内容,然后将当前页面的内容清除掉,然后再把list-view的内容呈现出来,并没有跳转到别的页面,你从头到尾都是在这个页面里,不过url地址会变化,因此看起来就像你到了另一个页面,这样给人的用户体验特别好,因为不需要等待页面加载过程.

说了这么多,我们来根据他的原理做一个SPA的小应用吧(里面的html和css代码直接复制了我之前看的那个博客的作者的,因为懒得自己设计)

html代码如下:

    <!DOCTYPE html>
<html>
  <head>
    <title>SPA</title>
    <link rel="stylesheet" type="text/css" href="index.css" />
    <script type="text/javascript" src="jquery-3.1.1.min.js"></script>
    <script type="text/javascript" src="spa.js"></script>
  </head>
  <body>
    <div class="pageview" style="background: #3b76c0" id="main-view">
      <h3>首页</h3>
      <div title="-list-view" class="right-arrow"></div>
    </div>
    <div class="pageview" style="background: #58c03b;display: none" id="list-view">
      <h3>列表页面</h3>
      <div class="left-arrow"></div>
      <div title="-detail-view" class="right-arrow"></div>
    </div>
    <div class="pageview" style="background: #c03b25;display: none" id="detail-view">
      <h3>列表详情页面</h3>
      <div class="left-arrow"></

转载于:https://www.cnblogs.com/libin-1/p/6002268.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值