search.html 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. {extend name="layout/base" /}
  2. {block name="header"}
  3. <link rel="stylesheet" href="__STATIC__/css/mobile/style.css">
  4. {/block}
  5. {block name="content"}
  6. <!-- page top start -->
  7. <header class="top">
  8. <div class="header search-page">
  9. <a href="javascript:history.back(-1)"><img src="__STATIC__/images/mobile/icon/left.png"></a>
  10. <div class="search">
  11. <input type="text" placeholder="大家都在搜“空之轨迹”" autocomplete="off" class="search-content" id="keywords">
  12. <img src="__STATIC__/images/mobile/icon/close-01.png" class="cancel">
  13. </div>
  14. <a href="javascript:;"><img src="__STATIC__/images/mobile/icon/search.png" class="input-search" id="search"></a>
  15. </div>
  16. </header>
  17. <div class="warp-search-cont">
  18. <ul class="search_cont" id="search_ul">
  19. <li><a href="">三国志</a></li>
  20. <li><a href="">三国志2017</a></li>
  21. <li><a href="">三国志大战</a></li>
  22. <li><a href="">放开那三国志</a></li>
  23. </ul>
  24. </div>
  25. <!-- page top end -->
  26. <div class="hot-search top-half">
  27. <div class="title">
  28. <img src="__STATIC__/images/mobile/icon/hot-search.png">
  29. <h4>热门搜索</h4>
  30. </div>
  31. <div>
  32. <a href="">亮剑三国</a>
  33. <a href="">忍者插班生</a>
  34. <a href="">新幻想群侠</a>
  35. <a href="">玄机</a>
  36. <a href="">精灵战纪</a>
  37. </div>
  38. </div>
  39. <div class="history-search">
  40. <div class="title">
  41. <div>
  42. <img src="__STATIC__/images/mobile/icon/history-search.png">
  43. <h4>搜索历史</h4>
  44. </div>
  45. <a href="javascript:;" class="clear-history">
  46. <img src="__STATIC__/images/mobile/icon/delete.png">
  47. <h4>清空历史</h4>
  48. </a>
  49. </div>
  50. <ul>
  51. <!-- <li>
  52. <a href="">阴阳师</a>
  53. <a href="javascript:;" class="delete"><img src="__STATIC__/images/mobile/icon/close.png"></a>
  54. </li>
  55. <li>
  56. <a href="">随便搜搜</a>
  57. <a href="javascript:;" class="delete"><img src="__STATIC__/images/mobile/icon/close.png"></a>
  58. </li>
  59. <li>
  60. <a href="">亮剑上</a>
  61. <a href="javascript:;" class="delete"><img src="__STATIC__/images/mobile/icon/close.png"></a>
  62. </li> -->
  63. </ul>
  64. </div>
  65. {/block}
  66. {block name="detail_js"}
  67. <script>
  68. document.getElementById("keywords").focus();
  69. $(".search-content").keyup(function() {
  70. var text = $(this).val();
  71. text = text.replace(/\s*/g, "");
  72. if (text != "") {
  73. $(this).next().show();
  74. $(".warp-search-cont").show();
  75. } else {
  76. $(this).next().hide();
  77. $(".warp-search-cont").hide();
  78. }
  79. })
  80. $(".cancel").click(function() {
  81. $(this).prev().val("");
  82. $(this).hide();
  83. $(".warp-search-cont").hide();
  84. $(".search_cont").html("");
  85. })
  86. // 粘贴事件监控
  87. $.fn.pasteEvents = function(delay) {
  88. if (delay == undefined) delay = 10;
  89. return $(this).each(function() {
  90. var $el = $(this);
  91. $el.on("paste", function() {
  92. $el.trigger("prepaste");
  93. setTimeout(function() {
  94. $el.trigger("postpaste");
  95. }, delay);
  96. });
  97. });
  98. };
  99. // 使用
  100. $(".search-content").on("postpaste", function() {
  101. var text = $(this).val();
  102. text = text.replace(/\s*/g, "");
  103. if (text != "") {
  104. $(this).next().show();
  105. $(".warp-search-cont").show();
  106. } else {
  107. $(this).next().hide();
  108. $(".warp-search-cont").hide();
  109. }
  110. }).pasteEvents();
  111. $(".warp-search-cont").click(function(e) {
  112. var target = $(e.target);
  113. if (target.closest(".search-content").length != 0) return;
  114. $(".warp-search-cont").hide();
  115. })
  116. // 搜索历史
  117. if($(".history-search ul li").length ==0){
  118. $(".history-search").hide();
  119. }else{
  120. $(".history-search").show();
  121. }
  122. $(function(){
  123. update_history();
  124. // 绑定回车事件
  125. $(document).keydown(function(event){
  126. if(event.keyCode==13){
  127. $("#search").click();
  128. }
  129. });
  130. // 搜索点击事件
  131. $("#search").click(function(){
  132. var keywords = $("#keywords").val();
  133. keywords= keywords.replace(/\s*/g, "");
  134. history(keywords); //添加到缓存
  135. update_history(); //更新搜索历史
  136. if (keywords == ""){
  137. keywords ="空之轨迹";
  138. }
  139. location.href = "";
  140. })
  141. // 清空搜索历史
  142. $(".clear-history").click(function(){
  143. mystorage.remove('keywords');
  144. $(".history-search ul").html(" ");
  145. $(".history-search").hide();
  146. })
  147. })
  148. /**
  149. * [update_history 更新搜索历史]
  150. * @return {[type]} [description]
  151. */
  152. function update_history(){
  153. // console.log(mystorage.get("keywords"));
  154. var history = mystorage.get("keywords");
  155. if (history) {
  156. var html = "";
  157. $.each(history,function(i,v){
  158. html += "<li><a href=''>"+v+"</a><a href='javascript:;' class='delete'><img src='/static/images/mobile/icon/close-gray.png'></a></li>"
  159. })
  160. $(".history-search ul").html(html);
  161. };
  162. if($(".history-search ul li").length ==0){
  163. $(".history-search").hide();
  164. }else{
  165. $(".history-search").show();
  166. }
  167. // 点击删除历史
  168. $(".delete").click(function(){
  169. var tqtest = $(this).prev().html();
  170. var history = mystorage.get("keywords");
  171. history.splice($.inArray(tqtest,history),1);
  172. mystorage.set("keywords",history);
  173. // $(this).parents("li").remove();
  174. update_history();
  175. })
  176. }
  177. /**
  178. * [history //搜索历史函数存储]
  179. * @param {[type]} value [搜索词]
  180. * @return {[type]} [description]
  181. */
  182. function history(value){
  183. var data = mystorage.get("keywords");
  184. if (!data) {
  185. var data = []; //定义一个空数组
  186. }else if(data.length === 10){ //搜索历史数量
  187. data.shift(); //删除数组第一个元素有
  188. }else{
  189. };
  190. if (value) { //判断搜索词是否为空
  191. if (data.indexOf(value)<0) { //判断搜索词是否存在数组中
  192. data.push(value); //搜索词添加到数组中
  193. mystorage.set("keywords",data); //存储到本地化存储中
  194. };
  195. };
  196. }
  197. /**
  198. * [mystorage 存储localstorage时候最好是封装一个自己的键值,在这个值里存储自己的内容对象,封装一个方法针对自己对象进行操作。避免冲突也会在开发中更方便。]
  199. * @param {String} ){ var ms [description]
  200. * @return {[type]} [description]
  201. console.log(mystorage.set('tqtest','tqtestcontent'));//存储
  202. console.log(mystorage.set('aa','123'));//存储
  203. console.log(mystorage.set('tqtest1','tqtestcontent1'));//存储
  204. console.log(mystorage.set('tqtest1','newtqtestcontent1'));//修改
  205. console.log(mystorage.get('tqtest'));//读取
  206. console.log(mystorage.remove('tqtest'));//删除
  207. mystorage.clear();//整体清除
  208. */
  209. var mystorage = (function mystorage(){
  210. var ms = "mystorage";
  211. var storage=window.localStorage;
  212. if(!window.localStorage){
  213. alert("浏览器不支持localstorage");
  214. return false;
  215. }
  216. var set = function(key,value){
  217. //存储
  218. var mydata = storage.getItem(ms);
  219. if(!mydata){
  220. this.init();
  221. mydata = storage.getItem(ms);
  222. }
  223. mydata = JSON.parse(mydata);
  224. mydata.data[key] = value;
  225. storage.setItem(ms,JSON.stringify(mydata));
  226. return mydata.data;
  227. };
  228. var get = function(key){
  229. //读取
  230. var mydata = storage.getItem(ms);
  231. if(!mydata){
  232. return false;
  233. }
  234. mydata = JSON.parse(mydata);
  235. return mydata.data[key];
  236. };
  237. var remove = function(key){
  238. //读取
  239. var mydata = storage.getItem(ms);
  240. if(!mydata){
  241. return false;
  242. }
  243. mydata = JSON.parse(mydata);
  244. delete mydata.data[key];
  245. storage.setItem(ms,JSON.stringify(mydata));
  246. return mydata.data;
  247. };
  248. var clear = function(){
  249. //清除对象
  250. storage.removeItem(ms);
  251. };
  252. var init = function(){
  253. storage.setItem(ms,'{"data":{}}');
  254. };
  255. return {
  256. set : set,
  257. get : get,
  258. remove : remove,
  259. init : init,
  260. clear : clear
  261. };
  262. })();
  263. </script>
  264. {/block}