①在一个页面上实现网站的大部分功能,就是单页面应用程序,是一种常见的网页开发模式。javascript
②整个网站就只有一个Html文件,每次在切换页面时,不须要请求服务器,只要经过本地的js来切换便可。这样能够减少服务器压力、加强用户体验,增长app的使用流畅性。html
①优势:前端
②缺点:java
①单页应用确定是要使用一些框架的,好比Vue、Angular、React等,可是使用 Vue、Angular、React 也不必定是作单页,作单页必定是先后端分离的方式,若是有 SEO 需求,则不要作成单页jquery
②具体使用的网站:网易云音乐、coding、Gmail等web
①historyAPI方案,参考以前的章:历史相关API
后端
②哈希(路由)方案:使用location.hash和hashchange事件实现路由服务器
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>模拟单页面应用</title> <style> *{ padding: 0; margin: 0; } body{ background-color: #f7f7f7; font-family: Arial, Helvetica, sans-serif; } header{ background: #242424; border-bottom: #000; } .wrapper{ width: 1100px; height: 70px; margin: 0 auto; } .wrapper h1{ float: left; width: 176px; height: 69px; background: url("topbar.png") no-repeat 0 0; font-size: 0; } .wrapper ul{ list-style: none; } .wrapper ul li{ float: left; height: 70px; } .wrapper ul li.now, .wrapper ul li:hover{ background: red; } .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><a href="#/">发现音乐</a></li> <li><a href="#/my">个人音乐</a></li> <li><a href="#/friend">朋友</a></li> </ul> </div> </header> <!-- 其它页面都须要显示在这个容器中 --> <div class="container" id="container"></div> <script src="jquery.js"></script> <script> // 经过注册 window.onhashchange 事件来监听 hahs(锚点)的改变 // url 地址发生改变以后,就解析 hash 中的路径标识 // 而后根据不一样的路径标识渲染不一样的页面到单页面中的容器中 window.onhashchange=function(){ var hash=window.location.hash.substr(1);//去除# if(hash==='/'){ $.get('./find.html',function(data){ $('#container').html(data); console.log(data) }); }else if(hash==='/my'){ $.get('./my.html',function(data){ $('#container').html(data); }); }else if(hash==='/friend'){ $.get('./friend.html',function(data){ $('#container').html(data); }); } } </script> </body> </html>