var upwardsLocked = false
// JQ 功能擴增
$.fn.triggerAll = function(list) {
return this.each(function() {
const $this = $(this)
$.each(list.split(' '), function(key, val) {
$this.trigger(val)
})
})
}
// 裝置判斷
function DeviceIsTouch() {
const userAgent = navigator.userAgent
const regexp = /Android|iPhone|SymbianOS|Windows Phone|iPad|iPod|Touch|Mobile|Tablet|BlackBerry/gi
return !!userAgent.match(regexp)
}
// resize 裝置判斷
;(function($) {
$(window).on('resize', function(e) {
if(DeviceIsTouch()) {
$('body').removeClass('DeviceWithHover')
} else {
$('body').addClass('DeviceWithHover')
}
})
})($)
//定義css變數 --vh 避免行動裝置vh計算異常(會被瀏覽器address bar影響)
$(function(){
function setVh(){
$('html').attr('style', `--vh: ${window.innerHeight * 0.01}px;`)
}
setVh()
$(window).on('resize', setVh)
})
// 天邊跑馬燈設定
;(function($) {
const marqueeTarget = $(".top-marquee .marquee-list")
if(marqueeTarget.length && marqueeTarget.children('.marquee-item').length > 1) {
$(".top-marquee .marquee-list").owlCarousel({
items: 1,
nav: false,
dots: false,
autoplay: true,
autoplayTimeout: 3000,
autoplaySpeed: 1000,
autoplayHoverPause: true,
animateIn: 'slideInUp',
animateOut: 'slideOutUp',
mouseDrag: false,
loop: true,
});
}
//關掉
if($(".top-marquee").length > 0){
$(document).on('click', '.marquee-toggle', function(){
$('.top-marquee').remove()
$('body').addClass('no-marquee')
})
}
})($)
// 螢幕滾動
;(function($) {
$(window).on('scroll', function(e) {
if($(this).scrollTop() > 0) {
$('.gotop').fadeIn(400)
$("body").addClass('is-sticky');
} else {
$('.gotop').fadeOut(400)
$("body").removeClass('is-sticky');
}
})
})($)
// 主選單
;(function($) {
let navbarStep = ''
function initMenu(){
$('body').removeClass('menu-open overflow-hidden');
$('body').off('click', '.navbar-toggle')
$('.navbar-main').off('click', '.main-menu a')
.find('.with-children').removeClass('in-active')
}
//關閉桌機版分類
$(document).on("click", ".subnav-closer", function(){
$(this).parents(".subnav-item").removeClass("in-active")
})
$(window).on('resize', function(e) {
if($(this).width() >= 1200 && navbarStep !== "desktop") {
navbarStep = "desktop"
initMenu()
$('.navbar-main').on('click', '.main-menu a', function(e) {
if(($(this).hasClass('main-link') || $(this).hasClass('subnav-link')) && $(this).parent().hasClass('with-children')) {
// e.preventDefault()
// $(this).parent('.with-children').toggleClass('in-active')
// .siblings('.with-children').removeClass('in-active')
//桌機三層選單觸邊
if($(this).hasClass('subnav-link') && ($(window).width() - $(this)[0].getBoundingClientRect().right < 340 )){
$(this).parent('.with-children').find('.thirdnav-wrapper').addClass("theme-edge")
}
}
})
} else if($(this).width() < 1200 && navbarStep !== "mobile") {
navbarStep = "mobile"
initMenu()
$('body').on('click', '.navbar-toggle', function(e) {
e.preventDefault()
$('body').toggleClass('menu-open overflow-hidden');
})
$('.navbar-main').on('click', '.main-menu a', function(e) {
if(($(this).hasClass('main-link') || $(this).hasClass('subnav-link')) && $(this).parent().hasClass('with-children')) {
e.preventDefault()
$(this).parent('.with-children').toggleClass('in-active')
.siblings('.with-children').removeClass('in-active')
}
})
}
})
})($)
// gotop
;(function($) {
$('.gotop').on('click', function(e){
e.preventDefault()
$("html, body").animate({scrollTop: 0},800);
});
})($)
// contactbox
;(function($) {
$('.contactbox-toggle').on('click', function(e){
$('.contactbox-wrapper').toggleClass('in-active')
});
})($)
// ==========================================================
// 搜尋框開啟
// ==========================================================
;(function($) {
$(document).on('click', '.func-search', function(){
let icon = $(this).find('.icon')
searchWrapper = $('.navbar-search')
if(searchWrapper.hasClass('in-active')){
$('.navbar-search').removeClass('in-active')
}else{
$('.navbar-search').addClass('in-active')
}
})
})($)
//========================================
// 通用輪播
//========================================
function commonCarousel(target){
target.owlCarousel({
loop:false,
items: 1,
dots: false,
nav: true,
navText: ['', ''],
responsive: {
0: {
items: 2,
margin: 12,
stagePadding: 50
},
675: {
items: 3,
margin: 12,
stagePadding: 60
},
1200: {
items: 4,
margin: 15,
stagePadding: 75
},
1600: {
items: 5,
margin: 20,
stagePadding: 100
},
}
})
}
function commonCarouselSmall(target){
target.owlCarousel({
loop:false,
items: 1,
dots: false,
nav: true,
navText: ['', ''],
responsive: {
0: {
items: 2,
margin: 12,
stagePadding: 50
},
675: {
items: 3,
margin: 12,
stagePadding: 60
},
768: {
items: 4,
margin: 15,
stagePadding: 75
},
1600: {
items: 5,
margin: 20,
stagePadding: 100
},
}
})
}
// ==========================================================
// owl carousel 樣式校正
// ==========================================================
;(function($) {
function dotsCheck(jqElement) {
const thisTarget = jqElement
const checkTarget = thisTarget.find('.owl-dots')
if(!checkTarget.hasClass('disabled')) {
thisTarget.addClass('with-dots')
} else {
thisTarget.removeClass('with-dots')
}
}
$('body').on('refreshed.owl.carousel', '.owl-carousel.with-dots', function(e) {
dotsCheck($(this))
})
})($)
// ==========================================================
// footer menu toggle
// ==========================================================
;(function($, jQuery, window, document) {
let footerMenuStep = '';
$(window).on('resize', function(e) {
if($(this).width() >= 1200 && footerMenuStep !== 'desktop') {
footerMenuStep = 'desktop'
$('.footer-menu').off('click', '.menu-title').find('.menu-item').removeClass('in-active')
} else if($(this).width() < 1200 && footerMenuStep !== 'mobile') {
footerMenuStep = 'mobile'
$('.footer-menu').on('click', '.menu-title', function(e) {
e.preventDefault()
$(this).parent('.menu-item').toggleClass('in-active')
.siblings('.menu-item').removeClass('in-active')
})
}
})
})($, jQuery, window, document)
// ==========================================================
// 通用跳窗功能
// ==========================================================
;(function($) {
$('body').on('click', '.popupBox-wrapper', function(e) {
if(e.currentTarget === e.target || $(e.target).hasClass('closer') || $(e.target).parent().hasClass('closer')) {
e.preventDefault()
let popupWrapper = '';
$('body').removeClass('overflow-hidden')
if(e.currentTarget === e.target) {
popupWrapper = $(e.target)
} else {
popupWrapper = $(e.target).parents('.popupBox-wrapper')
}
if(!popupWrapper.hasClass('popupBox-constant')) {
popupWrapper.remove()
} else {
popupWrapper.addClass('hide')
}
}
})
$(document).on('click', '.popupBox-trigger', function(){
let _this = $(this)
target = _this.data("target"),
popupTarget = $('.popupBox-wrapper[data-popup=' + target + ']')
if(popupTarget.length > 0){
if(!(($(window).width() < 992) && _this.hasClass('mobile-disable-popup'))){
$('body').addClass('overflow-hidden')
}
popupTarget.removeClass('hide')
}
})
})($)
// ==========================================================
// 再買一次
// ==========================================================
;(function($) {
//再買一次
$(document).on("click",".Order_Again",function(){
const order_num = $(this).data("order");
$.ajax({
url:Project_Country+"member/ajax/ajax_order_again.php",
type:"POST",
data:{order_num:order_num,},
dataType:'json',
error:function(d){
alert('網路連線過慢,網頁請重新整理');
},
success:function(d)
{
if(d.status != 'ok')
{
alert(d.msg);
}
else
{
if(d.dataLayer)eval(d.dataLayer);
window.location.href = d.url;
}
}
});
});
})($)
// ==========================================================
// 快速購物跳窗
// ==========================================================
;(function($) {
let commonShopcartStep = false
$('body').on('click', '.QuickShop', function(e) {
e.preventDefault()
if(!commonShopcartStep) {
commonShopcartStep = true
jQuery.ajax({
url: Project_Country + 'ajax/ajax_popup_shopbox.php',
type: 'POST',
dataType: 'html',
data: {
SID: $(this).attr('SID'),
Serial: $(this).attr('serial'),
Amount: $(this).parent().find('.amountBox').val(),
},
}).done(function(res) {
if(!res.includes("window.location.href")){
$('body').addClass('overflow-hidden')
}
$('body').append(res)
const slideTarget = $('.quickShop-box .preview-slide')
if(slideTarget.length && slideTarget.children('.slide-item').length > 1) {
slideTarget.addClass('with-dots').owlCarousel({
nav: true,
navText: ['', ''],
dots: false,
margin: 10,
items: 1,
})
}
}).fail(function(x, y, z) {
console.log(x)
}).always(function() {
commonShopcartStep = false
})
}
})
})($)
// 列表更多資訊開關
;(function($) {
$('main').on('click', '.toggle-expand', function(e) {
e.preventDefault();
let parent = $(this).parents('.table-row')
if(parent.hasClass("in-expand")){
$(".table-row").removeClass('in-expand')
}else{
$(".table-row").removeClass('in-expand')
parent.addClass('in-expand')
parent.prev(".table-row-normal").addClass('in-expand')
parent.next(".table-notebook").addClass('in-expand')
}
})
})($)
// ==========================================================
// 側欄選單開關
// ==========================================================
;(function($) {
$('.aside-panel').on('click', 'a', function(e) {
if($(this).parent().hasClass('with-children')) {
e.preventDefault()
$(this).parent('.with-children').toggleClass('in-active')
}
})
$('.menu-switch').on('click', function(e){
e.preventDefault()
$(this).parents('.with-children').eq(0).toggleClass('in-active')
.siblings('.with-children').removeClass('in-active')
})
$('.aside-panel').on('click', '.collapse-title', function(e) {
e.preventDefault()
$(this).parent('.item').toggleClass('in-active')
})
$('.panel-foldable').on('click', '.panel-head', function(){
$(this).parent('.aside-panel').toggleClass('in-active')
})
})($)
// ==========================================================
// footer 訂閱電子報功能
// ==========================================================
;(function($) {
if($('#EpaperForm').length) {
$("body").on('submit', '#EpaperForm', function(e){
e.preventDefault()
const enter_str = check_tools.checkEmailNull($("#Epaper_Email"));
if( enter_str == "NULL" ){
alert(_jsLang.請輸入Email);
return false;
}else if( enter_str == "ERROR" ){
alert(_jsLang.您輸入的Email格式錯誤);
return false;
}else if( enter_str == "PASS" ){
$.ajax({
url: Project_Country + 'include/ajax/ajax_epaper-p.php',
type: 'POST',
dataType: 'JSON',
data: {
Epaper_Email: $("#Epaper_Email").val()
},
})
.done(function(res) {
const status = res.status
if(status === "ORDER") {
alert(_jsLang.訂閱電子報成功);
} else if (status === "CANCEL") {
alert(_jsLang.取消訂閱電子報成功);
} else if (status === "Usable") {
alert(_jsLang.訂閱電子報成功抵用券序號已寄送至您的信箱);
} else if(status === "ERROR") {
alert(_jsLang.取消訂閱電子報失敗);
}
})
.fail(function(x, y, z) {
console.log(x);
})
.always(function() {
$("#Epaper_Email").val('');
});
}
});
}
})($)
// ==========================================================
// 購物車簡易浮動跳窗
// ==========================================================
function FloatShopcart(res) {
const returnDOM = `
${res}
`
return returnDOM
}
;(function($) {
const shopcartTarget = $('#Shop_Cart_Total')
let CanShowShopcart = true
shopcartTarget.on('mouseenter', function(e) {
if(CanShowShopcart) {
CanShowShopcart = false
$.ajax({
url: Project_Country + "ajax/ajax_get_cart.php",
type: 'GET',
dataType: 'HTML',
})
.done(function(res) {
shopcartTarget.append(FloatShopcart(res))
})
.fail(function(x, y, z) {
console.log(x);
})
.always(function() {
CanShowShopcart = true
});
}
}).on('mouseleave', function(e) {
shopcartTarget.children('.float-shopcart').remove()
})
// 浮動窗中商品刪除
$('body').on('click', '.Left_Top_Del', function(e) {
e.preventDefault()
// if(confirm(_jsLang.確定刪除嗎+"?")){
$.ajax({
url: Project_Country + "shopcart/ajax/ajax_cart_del.php",
type:"POST",
cache:false,
dataType:'json',
data:{ID:$(this).attr("sid"),Custom_Prod_Info_String:$(this).attr("Custom_Prod_Info_String")},
}).done(function(d){
if(d.Status == 'RET_SUCCESS'){
// alert(_jsLang.刪除成功);
window.location.reload();
}else{
alert(_jsLang.資料庫忙線中);
}
}).fail(function(d){
alert('網路連線過慢,網頁請重新整理');
})
// }
})
})($)
function FloatCartPopup(dataObj) {
const shopcartTarget = $('#Shop_Cart_Total')
$.ajax({
url : Project_Country + "ajax/ajax_get_cart.php",
type : "POST",
async : true,
cache : false,
data : dataObj,
}).done(function(res) {
shopcartTarget.append(FloatShopcart(res))
setTimeout(function(){
shopcartTarget.children('.float-shopcart').remove()
},3000);
}).fail(function(x, y, z) {
console.log(x)
})
}
// ==========================================================
// 影片預設圖 to iframe 撥放器
// ==========================================================
;(function($) {
$(document).on('click', '.StaticVideo', function(e) {
e.preventDefault()
const targetVID = $(this).attr('data-vid')
const targetType = $(this).attr('data-type')
let videoChild = ''
switch(targetType) {
case 'youtube':
default:
videoChild = ``
break;
case 'facebook':
videoChild = ``
break;
}
$(this).parents('.responsive-embed').html(videoChild)
})
})($)
// 商品大小格切換
;(function($) {
if($('.switch-group').length) {
const themeAry = []
$('.switch-group').find('.switch-item').each(function(i, ele) {
const themeItem = $(ele).attr('data-target')
themeAry.push(themeItem)
})
$('.switch-group').on('click', '.switch-item', function(e) {
const targetClass = $(this).data('target')
$(this).addClass('now')
.siblings('.switch-item').removeClass('now')
$(this).parents('main').find('.items-list').removeClass(themeAry.join(' ')).addClass(targetClass)
})
}
})($)
// ==========================================================
// 商品規格 tag 切換
// ==========================================================
;(function($) {
$('body').on('click', '.tag-box .title-link', function(e) {
e.preventDefault()
const thisTarget = $(this).data('target')
$(this).parent('.title-item').addClass('in-active')
.siblings('.title-item').removeClass('in-active')
$('.popupBox-wrapper[data-popup=' + thisTarget + ']').addClass('in-active')
.siblings('.popupBox-wrapper').removeClass('in-active')
})
})($)
// ==========================================================
// 商品規格評論區塊腳本
// ==========================================================
;(function($) {
// 輪播功能
let productReviewStep = ''
$(window).on('resize', function(e) {
if($(this).width() >= 1200 && productReviewStep !== 'desktop') {
productReviewStep = 'desktop'
$('.content-pics').each(function(i, ele) {
if($(ele).children().length > 3) {
$(ele).owlCarousel({
margin: 20,
nav: true,
dots: false,
navText: ['', ''],
items: 3
})
}
});
} else if($(this).width() < 1200 && productReviewStep !== 'mobile') {
productReviewStep = 'mobile'
$('.content-pics').each(function(i, ele) {
if($(ele).data('owl.carousel')) {
$(ele).data('owl.carousel').destroy()
}
});
}
})
// 放大圖
$(document).on('click', '.content-pics .OpenSlide', function(e) {
e.preventDefault()
$('body').addClass('overflow-hidden')
const targetPic = $(this).attr('href');
const targetGroup = $(this).attr('group')
const targetStep = $(this).attr('data-step')
const groupAry = $(this).parents('.content-pics').find('[group=' + targetGroup + ']')
const slideAry = []
groupAry.each(function(i, ele) {
const pushString = $(ele).attr('href')
slideAry.push(pushString)
});
// console.log(slideAry)
const appendSlides = slideAry.reduce(function(prev, ele, i) {
const thisStr = `
`
return prev + thisStr
}, '')
const appendChild = `
`
$('body').append(appendChild)
})
// 滑動到指定區塊
$(document).on('click', '.anchor-btn', function(e) {
e.preventDefault()
let _this = $(this)
const clickTarget = _this.data('target')
if($(clickTarget).length) {
const scrollTop = $(clickTarget).offset().top - $('.header-wrapper').height()
if(_this.data('click')){
$(clickTarget).trigger('click')
}
$('html, body').animate({
scrollTop
}, 750)
}
})
})($)
// ==========================================================
// 桌機版商品放大功能
// ==========================================================
;(function($) {
if(!DeviceIsTouch()) {
$(document).on('click', '.zoom-link', function(e) {
e.preventDefault()
}).on('mouseenter', '.zoom-link', function(e) {
e.preventDefault()
const thisPic = $(this).attr('href')
$(this).before(`
`)
}).on('mousemove', '.zoom-link', function(e) {
e.preventDefault()
const targetImg = $(this).siblings('.zoomin-picture')
const targetCursor = $(this).siblings('.zoomin-cursor')
const imgHeight = $(targetImg).height()
const imgWidth = $(targetImg).width()
$(targetImg).css({
top: (e.offsetY / $(this).height() * imgHeight * -1 + e.offsetY),
left: (e.offsetX / $(this).width() * imgWidth * -1 + e.offsetX)
})
$(targetCursor).css({
top: e.offsetY,
left: e.offsetX
})
}).on('mouseleave', '.zoom-link', function(e) {
e.preventDefault()
$(this).siblings('.zoomin-picture, .zoomin-cursor').remove()
})
}
})($)
// ==========================================================
// 摺疊table開關功能
// ==========================================================
;(function($) {
$(document).on('click', '.CollapseToggle', function(e) {
e.preventDefault()
const $targetBox = $(this).parents('.table-row')
if($targetBox.hasClass('table-controls')) {
$targetBox.toggleClass('in-expand');
} else {
$targetBox.prev('.table-controls').toggleClass('in-expand');
}
})
})($)
// ==========================================================
// 通用型滑動到指定位置
// ==========================================================
;(function($) {
$(document).on('click', '.ScrollToTarget', function(e) {
e.preventDefault()
const scrollTarget = $(this).attr('data-target')
if($(scrollTarget).length) {
const scrollTop = $(scrollTarget).offset().top - $('.header-wrapper').height()
$('html, body').animate({
scrollTop
}, 750)
}
})
})($)
// ==========================================================
// 密碼顯示切換
// ==========================================================
;(function($) {
$(document).on('click', '.password-toggle', function(e) {
e.preventDefault()
const target = $(this).siblings('input')
const targetType = target.attr('type')
$(this).find('.icon').toggleClass('icon-eye').toggleClass('icon-eye-slash')
if(targetType === 'password') {
target.attr('type', 'text')
} else if(targetType === 'text') {
target.attr('type', 'password')
}
})
})($)
// trigger function
;(function($) {
$(window).triggerAll('resize scroll')
})($)
/*影片效果*/
function videoBox(){
//影片控制
$(document).on('click','.videoControl',function(e){
e.preventDefault()
const targetVideo = $($(this).attr('data-target'))[0]
if(targetVideo.paused){
$(this).find('.fa').addClass('fa-pause').removeClass('fa-play');
targetVideo.play();
} else {
$(this).find('.fa').addClass('fa-play').removeClass('fa-pause');
targetVideo.pause();
}
});
// //影片預覽圖
// if(document.getElementById("Video")){
// document.getElementById("Video").oncanplay=function(){
// var video = $("#Video").get(0);
// var canvas = document.getElementById("video_review");
// canvas.getContext('2d').drawImage(video, 0, 0, 66, 92);
// };
// }
}
function LeadingZero(input, strLength = 2) {
let str = String(input)
if(str.length < strLength) {
str = '0' + str
// console.log(str)
return LeadingZero(str, strLength)
} else {
return str
}
}
function CountdownTimer(limitTime, runFunc = () => false) {
const now = Date.now()
if(limitTime > now) {
const leftTimes = limitTime - now
const leftSec = Math.floor(leftTimes / 1000) % 60
const leftMin = Math.floor(leftTimes / 1000 / 60) % 60
const leftHour = Math.floor(leftTimes / 1000 / 60 / 60)
runFunc(`
${_jsLang.倒數}
${LeadingZero(leftHour)}:${LeadingZero(leftMin)}:${LeadingZero(leftSec)}
`)
setTimeout(CountdownTimer, 1000, limitTime, runFunc)
} else {
runFunc(`${_jsLang.活動結束}`)
}
}
function CountDown(secondsTarget, runFunc = () => false) {
const seconds = secondsTarget.attr('second')
if(seconds > 0) {
const leftSec = Math.floor(seconds / 1) % 60
const leftMin = Math.floor(seconds / 1 / 60) % 60
const leftHour = Math.floor(seconds / 1 / 60 / 60)
runFunc(`
${_jsLang.倒數}
${LeadingZero(leftHour)}:${LeadingZero(leftMin)}:${LeadingZero(leftSec)}
`)
secondsTarget.attr('second', seconds - 1)
setTimeout(CountDown, 1000, secondsTarget, runFunc)
} else {
runFunc(`${_jsLang.活動結束}`)
}
}
// 關閉跳窗特效
function PopupCloseAnimate(jqElement) {
jqElement.fadeOut(250, function() {
jqElement.remove()
$('body').removeClass('overflow-hidden')
});
}
// 客製化 alert popup
function AlertPop(obj) {
const setting = {
backgroundColor: '',
color: '',
icon: '',
string: '',
}
if(obj) {
Object.assign(setting, obj)
}
let customStyle = ''
customStyle += (setting.backgroundColor)? `background-color: ${setting.backgroundColor};`: ''
customStyle += (setting.color)? `color: ${setting.color};`: ''
const alertStyle = (customStyle)? `style="${customStyle}"`: ''
const customIcon = (setting.icon)? `${setting.icon}`: '';
const alertElement = `
${customIcon}
${setting.string}
`
$('body').addClass('overflow-hidden').append(alertElement)
const alertTarget = $('.alert-wrapper')
setTimeout(function() {
PopupCloseAnimate(alertTarget)
}, 1500)
}
// 自定義 confirm popup
function ConfirmPop(obj) {
const setting = {
titleName: '',
titleColor: '',
string: '',
trueFunction: () => false,
falseFunction: () => false,
extraClass: '',
}
if(obj) {
Object.assign(setting, obj)
}
const titleStyle = (setting.titleColor)? `class="confirm-title" style="color: ${setting.titleColor};"`: `class="confirm-title font-primary"`
const confirmTitle = (setting.titleName)? `${setting.titleName}
`: ''
const confirmElement = `
`
$('body').addClass('overflow-hidden').append(confirmElement)
$('.confirm-popup').on('click', '.btn', function(e) {
e.preventDefault()
let returnValue = false
const targetWrap = $(this).parents('.confirm-popup')
PopupCloseAnimate(targetWrap)
if($(this).attr('data-value') === 'true') {
returnValue = true
} else {
returnValue = false
}
return (returnValue)? setting.trueFunction(): setting.falseFunction()
})
}
// ==========================================================
// 複製連結
// ==========================================================
$(function(){
$(document).on("click", ".copy-btn", function(e){
window.navigator.clipboard.writeText($(this).data("link"))
alert("複製連結成功")
})
})
$(function(){
$(".HITS_BT").on("click",function(e){
e.preventDefault();
if( $.isNumeric($(this).attr('hid')) && $.isNumeric($(this).attr('hdid')) ){
var hid = $(this).attr('hid');
var hdid = $(this).attr('hdid');
var url = $(this).attr('href');
var target = $(this).attr('target');
$.ajax({
url:Project_Country+"ajax/ajax_add_ad_hits-p.php",
type:"POST",
cache:false,
async:false,
data:{Hid: hid , Hdid : hdid},
error:function(d){
alert('網路連線過慢,網頁請重新整理');
},
success:function(d){
if(url){
if(target == '_blank'){
window.open(url);
}else if( ( typeof target === 'undefined' ) || (target == '') ){
window.location = url;
}else{
}
}
}
});
}else{
alert(_jsLang.這是錯誤的連結);
}
});
})
//========================
// 廣告行倒數(含跑馬燈)
//========================
$(function(){
let targetList = $(".countdown-wrapper")
targetList.each(function(){
countDown($(this))
})
function LeadingZero(input, strLength = 2) {
let str = String(input)
if(str.length < strLength) {
str = '0' + str
// console.log(str)
return LeadingZero(str, strLength)
} else {
return str
}
}
function countDown(target) {
let leftTime = target.data('second')
if(leftTime > 0) {
let leftSec = leftTime % 60,
leftMin = Math.floor(leftTime % 3600 / 60),
leftHour = Math.floor(leftTime % 86400 / 3600),
leftDate = Math.floor(leftTime / 86400)
if(leftDate > 0) {
target.html(`
${LeadingZero(leftDate)}
${_jsLang.天}
${LeadingZero(leftHour)}
${_jsLang.時}
${LeadingZero(leftMin)}
${_jsLang.分}
${LeadingZero(leftSec)}
${_jsLang.秒}
`)
} else {
target.html(`
${LeadingZero(leftHour)}
${_jsLang.時}
${LeadingZero(leftMin)}
${_jsLang.分}
${LeadingZero(leftSec)}
${_jsLang.秒}
`)
}
target.data('second', leftTime - 1)
setTimeout(countDown, 1000, target)
}
else
{
target.addClass("hide");
}
}
})
// ==========================================================
// 滾動事件節流
// ==========================================================
function throttleScroll(func, time, scrollBox = $(window)){
let throttleFlag = false
function throttle(method ,currentScroll){
if(throttleFlag){
return false
}
throttleFlag = true
setTimeout(function(){
//currentScroll為當前滾動的容器
method(currentScroll)
throttleFlag = false
}, time)
}
let tempFunc = function(){
throttle(func, $(this))
}
return {
init: function(){
scrollBox.on('scroll', tempFunc)
},
destroy: function(){
scrollBox.off('scroll', tempFunc)
}
}
}
// ==========================================================
// editor 索引化
// ==========================================================
;(function($) {
let contentIndexer = $(".content-indexer")
if(contentIndexer.length > 0){
contentIndexer.each(function(){
let _this = $(this)
let h2Index = 1
let h3Index = 1
const result = []
_this.find("h2").each(function(){
const h2Ele = $(this)
const h2Text = h2Ele.text()
let h2DataIndex = "h2-" + h2Index
h2Ele.attr("data-index", h2DataIndex)
const sub = []
h2Ele.nextUntil("h2","h3").each(function(){
const h3Ele = $(this)
const h3Text = h3Ele.text()
let h3DataIndex = "h3-" + h3Index
h3Ele.attr("data-index", h3DataIndex)
sub.push({
index: h3DataIndex,
text: h3Text
})
h3Index++
})
result.push({
index: h2DataIndex,
text: h2Text,
sub: sub
})
h2Index++
})
let h2Html = ''
for(id in result){
let h2Item = result[id]
let h3Html = ''
for(subId in h2Item.sub){
let h3Item = h2Item.sub[subId]
h3Html += `
${h3Item.text}
`
}
h2Html += `
${h2Item.text}
${h3Html}
`
}
_this.find("h2").eq(0).before(
``)
})
//展開
$(document).on("click", ".editor-index .index-switch", function(){
$(this).parents(".editor-index").toggleClass("theme-collapse")
})
//跳轉
$(document).on('click', '.editor-index .index-anchor', function(e) {
e.preventDefault()
let _this = $(this),
headerHeight = $('.header-wrapper').height(),
target = $('[data-index=' + _this.data("target") + ']')
if(target.length > 0){
$('html, body').animate({
scrollTop: target.offset().top - headerHeight - 10
})
}
})
}
})($)
// ==========================================================
// 開賣通知
// ==========================================================
;(function($) {
$(document).on("click", ".sale-notice-btn", function(){
$("#noticeID").val($(this).attr('SID'));
})
$(document).on("click", "#noticeSend", function(){
let email = $("#saleNoticeEmail").val();
if(!email)
{
alert('請輸入E-mail');
return false;
}
let emailRule = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z]+$/;
if(email.search(emailRule) == -1)
{
alert('E-mail格式錯誤');
return false;
}
$.ajax({
url:Project_Country+'products/ajax/common/ajax_add_prod_notice.php',
type:"POST",
cache:false,
async:false,
data:{SID:$("#noticeID").val(),email:email},
error:function(d){
alert('網路連線過慢,網頁請重新整理');
},
success:function(d){
if(d=='OK'){
alert("您已成功新增一筆開賣通知");
}else{
alert(_jsLang.資料庫忙線中);
}
$("#saleNoticeClose").trigger("click");
}
});
})
})($)