self.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. const Self = {
  2. init() {
  3. this.langEvent()
  4. this.productNavEvent()
  5. this.productDetailImgChange()
  6. this.aboutusEvent()
  7. this.headerShrinksEvent()
  8. this.indexPageMessageClick()
  9. this.formSubmitEvent()
  10. },
  11. langEvent() {
  12. $("#langSlected").click(() => {
  13. $("#langSlected").find(".dataList").toggle(500)
  14. })
  15. },
  16. productNavEvent() {
  17. $("#category").on('click', 'li', (e) => {
  18. console.log($(e.target))
  19. if ($(e.target).hasClass("title")) {
  20. $('#category').find("li").removeClass("active")
  21. $(e.target).parent("li").next(".category2").find('li').eq(0).addClass("active")
  22. } else {
  23. $('#category').find("li").removeClass("active")
  24. $(e.target).parent("li").addClass("active")
  25. }
  26. })
  27. },
  28. productDetailImgChange() {
  29. //默认选中第一个
  30. $(".img-group").find(".img-item").eq(0).addClass("active")
  31. const src = $(".img-group").find(".img-item").eq(0).find('img').attr('src')
  32. $("#imgShowLarge").attr("src", src)
  33. $(".img-group").find(".img-item").eq(0).click()
  34. var img_group_div = $("#projectlist").find(".img-group div")
  35. if(!img_group_div[0]) return
  36. var img_group_div_mar = 10 * img_group_div.length
  37. var img_group_width = (img_group_div[0].offsetWidth * img_group_div.length) + img_group_div_mar
  38. var img_group_box_width = $("#projectlist").find(".img-group-box").width()
  39. // 判断是否添加右边禁止点击
  40. if (!(img_group_width - img_group_box_width > 0)) {
  41. $("#projectlist").find('.img-change .right-btn').addClass('right-btn-ban')
  42. }
  43. $("#projectlist").find(".right-btn").click(() => {
  44. if (img_group_width - img_group_box_width > 0) {
  45. var shuzi = img_group_width - Math.abs(parseInt($("#projectlist").find(".img-group").css("marginLeft"))) - img_group_box_width;
  46. // 移除左边禁止点击
  47. $("#projectlist").find('.img-change .left-btn').removeClass('left-btn-ban')
  48. if (shuzi > img_group_box_width) {
  49. $("#projectlist").find(".img-group").css("marginLeft", -img_group_box_width + parseInt($("#projectlist").find(".img-group").css("marginLeft")))
  50. } else {
  51. $("#projectlist").find(".img-group").css("marginLeft", -(img_group_width - img_group_box_width))
  52. // 添加右边禁止点击
  53. $("#projectlist").find('.img-change .right-btn').addClass('right-btn-ban')
  54. }
  55. } else {
  56. return;
  57. }
  58. })
  59. $("#projectlist").find(".left-btn").click(() => {
  60. if (img_group_width - img_group_box_width > 0) {
  61. // 移除右边禁止点击
  62. $("#projectlist").find('.img-change .right-btn').removeClass('right-btn-ban')
  63. if (Math.abs(parseInt($("#projectlist").find(".img-group").css("marginLeft"))) > img_group_box_width) {
  64. $("#projectlist").find(".img-group").css("marginLeft",parseInt($("#projectlist").find(".img-group").css("marginLeft"))+img_group_box_width)
  65. } else {
  66. $("#projectlist").find(".img-group").css("marginLeft", "0px");
  67. // 添加右边禁止点击
  68. $("#projectlist").find('.img-change .left-btn').addClass('left-btn-ban')
  69. }
  70. } else {
  71. return;
  72. }
  73. })
  74. // 点击小图切换大图
  75. $("#projectlist").find(".img-group").on('click', 'div', function() {
  76. $(this).addClass("active").siblings().removeClass("active");
  77. const src = $(this).find('img').attr('src')
  78. $("#imgShowLarge").attr("src", src)
  79. });
  80. // 锚点
  81. $('.nav-anchor').find('a').click(function() {
  82. $('html,body').animate({scrollTop: ($($(this).attr("href")).offset().top - 130)}, 1000)
  83. })
  84. $(".nav-anchor").on('click', 'li', function() {
  85. $(this).addClass('active').siblings().removeClass('active')
  86. })
  87. },
  88. aboutusEvent() {
  89. // play
  90. if(!document.getElementById("video")) return
  91. $(".playbtn").click(() => {
  92. $(".playbtn").hide()
  93. document.getElementById("video").play()
  94. $("#video").attr("controls", "controls")
  95. })
  96. document.getElementById("video").addEventListener("ended", function() {}, false)
  97. },
  98. headerShrinksEvent() {
  99. // header收缩
  100. if (location.href.indexOf('home') !== -1) {
  101. // 如果是首页
  102. $("#header").removeClass("shrink")
  103. }
  104. $(window).scroll(function() {
  105. const scrollTop = $(window).scrollTop()
  106. if ( $(".shrink")[0]) {
  107. if (scrollTop < 40) {
  108. $(".shrink").css("height", "100px")
  109. $("#logo").find("img").css("height", "55px")
  110. } else {
  111. $("#logo").find("img").css("height", "30px")
  112. $(".shrink").css("height", "60px")
  113. }
  114. } else {
  115. $("#header").css("height", "100px")
  116. $("#logo").find("img").css("height", "55px")
  117. }
  118. if ($(document).height() > 2200) {
  119. if (scrollTop > 1050) {
  120. $(".left-con").find(".contact-us").css({"position": "fixed", "bottom": "420px"})
  121. $(".left-con").find(".message").css({"position": "fixed", "bottom": "20px"})
  122. } else {
  123. $(".left-con").find(".contact-us").css({"position": "relative", "bottom": "0"})
  124. $(".left-con").find(".message").css({"position": "relative", "bottom": "0"})
  125. }
  126. }
  127. })
  128. },
  129. indexPageMessageClick() {
  130. // 首页右侧信息点击
  131. // $("#messageLink").click(() => {
  132. // localStorage.setItem('message', true)
  133. // })
  134. // console.log(location.href)
  135. // if (location.href.indexOf('contact') !== -1 && localStorage.getItem('message')) {
  136. // $(window).scrollTop(850)
  137. // localStorage.removeItem('message')
  138. // }
  139. //首页右侧信息点击
  140. $("#messageLink").click(() => {
  141. $("#message-dialog").removeClass("dialog-hide")
  142. $(".dialog-mask").removeClass("dialog-hide")
  143. })
  144. $(".dialog-close").click(() => {
  145. $(".dialog").addClass("dialog-hide")
  146. $(".dialog-mask").addClass("dialog-hide")
  147. })
  148. },
  149. formAjax(params, sign, cb) {
  150. var url = '/api.php/cms/addmsg' //Api的地址,我这里是留言接口,如果自定义表单的话填写表单接口
  151. //定义data对象,默认包含appid,timestamp和signature
  152. var data = {
  153. appid: '{*pboot:appid*}',
  154. timestamp: '{*pboot:timestamp*}',
  155. signature: '{*pboot:signature*}'
  156. }
  157. //定义一个空数组,用来储存表单提交过来的内容
  158. var parm = new Array();
  159. //将表单提交过来的内容储存到数组中去
  160. for (var item in params) {
  161. parm[item] = $(params[item]).val()
  162. }
  163. //循环数组,将表单提交的内容添加到data对象中
  164. for (var i in parm) {
  165. data[i] = parm[i];
  166. }
  167. $.ajax({
  168. type: 'POST', //提交的类型为 POST
  169. url: url, //url地址为上面定义的url变量
  170. dataType: 'json', //提交的数据类型为 json
  171. data: data, //提交的数据为上面构建的数据
  172. //提交成功
  173. success: function( response, status ){
  174. $("#message-box").show()
  175. setTimeout(function() {
  176. $("#message-box").hide()
  177. }, 3000)
  178. //Api返回的内容,例如“留言成功”,“留言失败”等
  179. var Data = response.data;
  180. //如果成功,返回的code为1,失败返回0
  181. if( response.code == 1 ){
  182. $("#message-box").html(Data)
  183. $("#message-box").addClass('success')
  184. if (sign == 'download') {
  185. cb()
  186. }
  187. return false;
  188. } else {
  189. //留言失败,比如某个字段没有填写,返回XXX字段为空这样的提示。
  190. $("#message-box").html(Data)
  191. $("#message-box").addClass('warning')
  192. return false;
  193. }
  194. },
  195. //提交失败,将信息输出到控制台查看
  196. error: function( xhr, status, error ){
  197. $("#message-box").show()
  198. $("#message-box").html(error)
  199. $("#message-box").addClass('failed')
  200. setTimeout(() => {
  201. $("#message-box").hide()
  202. }, 3000)
  203. }
  204. })
  205. },
  206. formSubmitEvent() {
  207. $("#formBtn").click(() => {
  208. this.formAjax({
  209. content: '#formMessage',
  210. email: '#formEmail',
  211. contacts: '#formContacts',
  212. mobile: '#formMobile',
  213. company: '#formCompany'
  214. })
  215. })
  216. }
  217. }
  218. Self.init();