index.html 9.9 KB

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