Home > Web Front-end > JS Tutorial > Use native js to build a single page application

Use native js to build a single page application

高洛峰
Release: 2016-11-30 17:04:40
Original
1157 people have browsed it

The main idea

By changing the hash value of the url, jump to the corresponding module. First, display the default module and hide other modules. Define three hash values ​​for the three modules respectively. When clicking the option of the default module, change the hash value. At the same time, monitor the hashchange event on the window and perform corresponding module jump logic processing. . In this way, the forward and backward movement of the browser can be simulated, and the user experience is better.

Let’s take a look in detail below. There is now a scenario. The selection order is: car brand->car model->car series.

First the HTML part. The vehicle brand selection list is displayed by default, and the other two modules are hidden.

<div class="wrap">
  <div id="Brand">
    <div>品牌</div>
      <ul class="mycar_hot_list">
        <li>
          <p>大众</p>
        </li>
      </ul>
    </div>
    <div id="Type"  style="display:none">
      <dl>
      <dt>比亚迪汽车</dt>
      <dd>宋</dd>
    </dl>
  </div>
  <div id="Series" style="display:none">
    <ul class="mycar_datalist">
       <li>
         2013年款
       <li>
    </ul>
  </div>
</div>
Copy after login

js logic control part

① Define a variable object to store the data selected in the three modules, define the hash value, and the processing logic function of the corresponding module.

   info={
            brand:&#39;&#39;,
            carType:&#39;&#39;,
            carSeries:&#39;&#39;,
            pages:[&#39;Brand&#39;,&#39;Type&#39;,&#39;Series&#39;] 
        };
info.selectBrand=function(){
      document.title = &#39;选择商标&#39;;
      brandEvent();
}
//选择车型
info.selectType=function(){
      document.title = &#39;选择车型&#39;;
      document.body.scrollTop = 0;  //滚到顶部
       window.scrollTo(0, 0);
       typeEvent();  //为该模块的dom绑定事件或做其他逻辑
}
//选择车系
info.selectSeries=function(){
      document.title = &#39;选择车系&#39;;
      document.body.scrollTop = 0;
      window.scrollTo(0, 0);
      seriesEvent();
}
Copy after login

②dom binding event & other logic

  function brandEvent(){
//绑定跳转
   $(&#39;#Brand ul li&#39;).click(function(){
       info.brand=$(this).find(&#39;p&#39;).text();
       goPage(&#39;Type&#39;);
   })
  }
  function typeEvent(){
//绑定跳转
   $(&#39;#Type  dd&#39;).click(function(){
       info.carType=$(this).text();
       goPage(&#39;Series&#39;);
   })
  }
  function seriesEvent(){...}
Copy after login

③goPage logic jump control

function goPage(tag) {
    if ((tag == &#39;Brand&#39;)&&(location.hash.indexOf(&#39;Type&#39;)!=-1)){ // 后退操作
            history.back();
            document.title = &#39;选择商标&#39;; 
    }else if ((tag == &#39;Type&#39;)&&(location.hash.indexOf(&#39;Series&#39;)!=-1)){
            history.back();
            document.title = &#39;选择车型&#39;;
    }else {
        location.hash = tag;
    }
}
Copy after login

④js entry file (zepto.js is used here to select dom)

window.onload=function(){
        info.selectBrand();  //为默认显示的模块中的元素绑定相应的事件及其他逻辑
        $(window).on("hashchange", function (e) {
            doHashChange();
       });
}
Copy after login

⑤The most important hash change logic control

function doHashChange(){
    //获取hash的值
    var hash = location.hash.split(&#39;|&#39;)[0],
        tag = hash.replace(/#/g, &#39;&#39;);
    if (info.pages.indexOf(tag) == -1) {
        tag = &#39;Brand&#39;;
    }
    $(&#39;.wrap&#39;).children(&#39;div&#39;).hide();   
    //执行每个模块不同的方法
    if(typeof(info[&#39;select&#39; + tag]) == "function"){
        info[&#39;select&#39; + tag]();
    }
    //展示对应dom
    $(&#39;#&#39; + tag).show();
}
Copy after login


Related labels:
js
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template