/**
* 判斷圖片是否加載完畢,顯示默認圖片
* @param {type} imgObj
* @returns {undefined}
*/
function imgLoad(imgObj){
imgObj.each(function(i){
if (imgObj.eq(i).attr('dataimg') == undefined) {
return;
}
var val=imgObj.eq(i).attr('dataimg');
var img=new Image();
img.onload=function(){
if(img.complete==true){
imgObj.eq(i).attr('src',val);
}
}
//如果因爲網絡或圖片的原因發生異常,則顯示該圖片
img.onerror=function(){img.src='loading.gif'}
img.src = val;
})
}
//imgLoad($('#text_content img'));
/**
* 圖片預加載
* 判斷圖片是否加載完畢,顯示默認圖片
* @param {type} imgObj
* @returns {undefined}
*/
function lodingImg(){
$('#topbanner img,.com_list img').each(function(){
if($(this).offset().top<=$(document).scrollTop()+$(window).height()
&& $(this).offset().top>=$(document).scrollTop()) {
var url = $(this).attr('loding'),
img = $(this).attr('src');
if(img!=url){
$(this).attr({src:url,alt:''}).css({opacity:0}).animate({opacity:1},500);
}
}
});
}
lodingImg();//加載圖片
//窗口改變大小加載
$(window).resize(function(){
lodingImg();//加載圖片
});
//獲取滾動條當前的位置
function getScrollTop(){
var scrollTop = 0;
if (document.documentElement && document.documentElement.scrollTop){
scrollTop = document.documentElement.scrollTop;
}else if (document.body) {
scrollTop = document.body.scrollTop;
}
return scrollTop;
}
//獲取當前可是範圍的高度
function getClientHeight(){
var clientHeight = 0;
if (document.body.clientHeight && document.documentElement.clientHeight){
clientHeight = Math.min(document.body.clientHeight, document.documentElement.clientHeight);
} else {
clientHeight = Math.max(document.body.clientHeight, document.documentElement.clientHeight);
}
return clientHeight;
}
//獲取文檔完整的高度
function getScrollHeight(){
return Math.max(document.body.scrollHeight, document.documentElement.scrollHeight);
}
/**
* 滾動加載
*/
$(window).scroll(function(){
if (getScrollTop() + getClientHeight() >= getScrollHeight()){
nextPage();
}
lodingImg();
});
//返回頂部
$(window).scroll(function() {
var scrollTop = $(document).scrollTop();
if (scrollTop >= 300) {
$('#up').css({'display':'block'})
} else {
$('#up').css({'display':'none'})
}
});
//返回頂部
$('#up').click(function(event) {
$("html,body").animate({scrollTop:0}, 200);
});
/**
* 選項卡代碼展示
*/
$(".nav-tab li").click(function() {
var d=$(".nav-tab li").index(this)
$(".visa-details .visa-detail").hide();
$(".visa-details .visa-detail").eq(d).show();
$(".nav-tab li").removeClass('current');
$(this).addClass('current');
});
/**
* 註冊驗證函數
*/
function reg() {
//驗證姓名
var user = /^.*[\u4e00-\u9fa5]+.*$/;
if ($(".user").val() == "" || $(".user").val() == "爲方便覈實信息請填寫真實姓名") {
myalert("請填寫真實姓名");
return false;
} else if (!user.test($(".user").val())) {
myalert("請輸入中文姓名");
return false;
}
//驗證手機
var phone = /^1[3|4|5|8][0-9]\d{4,8}$/i;
if ($(".phone_input").val() == "" || $(".phone_input").val() == "請輸入中國大陸手機號") {
myalert("請輸入正確的手機號");
return false;
} else if (!phone.test($(".phone_input").val()) || $(".phone_input").val().length < 11) {
myalert("請輸入正確的手機號");
return false;
}
//驗證郵箱
var email = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/
if ($(".email").val() == "" || !email.test($(".email").val())) {
myalert("請輸入正確的郵箱");
return false;
}
//閱讀條款
if (!$('#Icheck').is(':checked')) {
myalert("請認真閱讀並同意以下條款");
return false;
}
return true;
}
/**
*點擊返回上一步
*/
$('.left_back').click(function(event) {
if(jQuery(this).attr("href")){
window.location.href = jQuery(this).attr("href");
return;
}
var refer=document.referrer;
if(!refer || refer ==window.location.href){
window.location.href = '/';
}else{
window.history.back();
}
});
/**
* 圖片100%顯示
* 去除圖片的style,width,height樣式
* 使得圖片自適應M端頁面
*/
function imgsFit() {
var imgs = document.getElementById('text_content').getElementsByTagName('img');
for(var i = 0 ; i < imgs.length ; i++){
imgs[i].removeAttribute("width");
imgs[i].removeAttribute("height");
imgs[i].removeAttribute('style');
imgs[i].setAttribute("width", "100%");
}
}
/**
* 異步加載
*/
var currentPage =1;
var pages = 8; //pages = {:$pageNum:}
function nextPage(){
if (pages == 0){
return;
}
currentPage++;
if(currentPage>pages){
jQuery('#no_more').show();
return;
}
if(currentPage>1){
jQuery('.loading').show();
}
var response = $.ajax({
type: "POST",
url: "/index.php?m=default&c=index&a=ghomeAjax",
data: {
"page" : currentPage,
},
success: function(data){
jQuery('.com_list ul').append(data);
jQuery('.loading').hide();
},
async: true
});
}
//滾動不斷進行加載
$(window).scroll(function(){
if (getScrollTop() + getClientHeight() >= getScrollHeight()){
nextPage();
}
lodingImg();
});
/**
* js驗證手機
*/
function checkPhone(phone){
if(!(/^1[3|4|5|7|8]\d{9}$/.test(phone))){
return false;
}
return true;
}
/**
* js驗證郵箱
*/
function checkEmail(str){
var re = /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/
if(re.test(str)){
return true;
}else{
return false;
}
}
/**
* js驗證QQ
*/
function isQQ(pQQ){
var bValidate = RegExp(/^[1-9][0-9]{4,9}$/).test(pQQ);
if (bValidate){
return true;
}else{
return false;
}
}
/**
* 判斷是否爲微信
*/
function isWeiXin(){
var ua = window.navigator.userAgent.toLowerCase();
if(ua.match(/MicroMessenger/i) == 'micromessenger'){
return true;
}else{
return false;
}
}
/**
* 判斷網站是否加載完成
* @type subSomething 加載狀態改變的時候執行這個方法
*/
document.onreadystatechange = subSomething;
function subSomething(){
//當頁面加載狀態
if(document.readyState == "complete") {
console.log("加載完成");
console.log(document.readyState);
}
}
/**
* 獲取頁面加載完成所需時間
* @returns loadTime 所需時間
*/
window.onload = function () {
var loadTime = window.performance.timing.domContentLoadedEventEnd-window.performance.timing.navigationStart;
console.log('Page load time is '+ loadTime);
}
/**
* 獲取頁面訪問所在地
* 引用新浪的js
* http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js
* @type remote_ip_info
*/
var mycity = remote_ip_info;
if(remote_ip_info['province']==remote_ip_info['city']){
console.log("您好,歡迎來自 "+remote_ip_info['city']+" 的網友");
}else{
console.log("您好,歡迎來自 "+remote_ip_info['province']+' '+remote_ip_info['city']+" 的網友");
}
/**
* 判斷兩個時間的大小
* @param {type} stime 開始時間
* @param {type} etime 結束時間
* @returns {Boolean}
*/
function contrastTime(stime,etime){
stime = stime.replace(/-/g,'/');
stime = stime.replace('T',' ');
var date = new Date(stime);
stime = date.getTime().toString();
stime = stime.substr(0, 10);
etime = etime.replace(/-/g,'/');
etime = etime.replace('T',' ');
date = new Date(etime);
etime = date.getTime().toString();
etime = etime.substr(0, 10);
return etime>=stime ? true : false;
}
/**
* 獲取當前日期時間
* @returns {String}
*/
function nowDate(){
var myDate = new Date();
return myDate.getFullYear()+'-'+doubleDigit(myDate.getMonth()+1)+'-'+doubleDigit(myDate.getDate())+' '+doubleDigit(myDate.getHours())+':'+doubleDigit(myDate.getMinutes());
}
/**
* 月份,天數,時間位數補零操作
* @param {type} num
* @returns {String}
*/
function doubleDigit(num){
return num<10 ? '0'+num : num;
}
/**
*@param 獲取當前時間戳
*/
function get_time_stamp(){
var timestamp = new Date().getTime();
//timestamp = timestamp/1000;
return timestamp;
}
/**
* 指定日期時間轉爲時間戳格式
* @param {type} dateStr
* @returns {String}
*/
function get_unix_time(dateStr){
var date = new Date(dateStr);
var time_str = date.getTime().toString();
//return time_str.substr(0, 10);
return time_str;
}
/**
* 二維碼生成
* 需要引入 jquery.qrcode.min.js
*/
function weixin(){
$('.wxBox').css("display","block");
//二維碼後面的黑色彈層
$('.bg').css("display","block");
//放置二維碼的容器
$('#qrcodeDiv').qrcode({
"width":180,
"height":180,
"correctLevel":0,
"text":window.location.href
});
jQuery('.float-share').hide();
var canvas = $('#qrcodeDiv canvas');
//生成二維碼圖片
var img = canvas.get(0).toDataURL("image/png");
var pHtml = '<img src="'+img+'" width="180" height="180"/>';
//把二維碼圖片放入容器中
$('#qrcodeDiv').html(pHtml);
}
/**
* 生成帶有logo的二維碼
* @param {type} img 即是logo的地址
*/
function logoQrcode(img){
jQuery('#output').qrcode({
text: window.location.href, //text: "www.58haha.cn"
height: 250,
width: 250,
correctLevel: 0,
src: img //這裏配置Logo的地址即可。
});
}
/**
* QQ,Sina,人人分享接口
* @param {type} type 分享的類型
* @param {type} url 分享的url
* @param {type} title 分享的標題
* @param {type} img 分享的圖片
* @param {type} content 分享的描述
*/
function shareys(type, url, title, img, content){
switch (type) {
case "sina":
url = "http://service.weibo.com/share/share.php?title=" + encodeURIComponent(content + '「' + title + '」' + ' 點這裏' + url) + '&pic=' + img;
window.open(url);
break;
case "tqq":
url = "http://share.v.t.qq.com/index.php?c=share&a=index&url=" + encodeURIComponent(url) + '&title=' + encodeURIComponent(title) + '&pic=' + img;
window.open(url);
break;
case "qzone":
url = 'http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url=' + encodeURIComponent(url) + '&title=' + encodeURIComponent(title) + '&site=&pics=' + encodeURIComponent(img) + '&desc=' + encodeURIComponent(content) +'&summary=' + encodeURIComponent(content);
window.open(url);
break;
case "ren":
url = 'http://widget.renren.com/dialog/share?resourceUrl=' + encodeURIComponent(url) + '&srcUrl=' + img + '&title=' + encodeURIComponent(title);
+'&description=' + content;
window.open(url);
break;
default:
break;
}
}
/**
* 頁面html代碼轉爲圖片
* 需要引入 html2canvas
*/
function html2Img(){
img.src='/qrcode.jpg';
var img = new Image();
img.onload = function(){
var elem = jQuery('#content');
html2canvas([elem.get(0)], {
onrendered: function (canvas) {
var image = canvas.toDataURL("image/jpg");
var pHtml = "<img src='"+image+"' style='width:100%;height:100%'>";
jQuery("#content").html(pHtml);
jQuery("#content").attr("style","padding-top:40px");
jQuery("#content").attr("style","padding-bottom:40px");
}
});
}
}
/**
* 校驗身份證號碼
* @param {type} code 身份證號
* @returns {Boolean}
*/
function IdentityCodeValid(code) {
var city={11:"北京",12:"天津",13:"河北",14:"山西",15:"內蒙古",21:"遼寧",22:"吉林",23:"黑龍江 ",31:"上海",32:"江蘇",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山東",41:"河南",42:"湖北 ",43:"湖南",44:"廣東",45:"廣西",46:"海南",50:"重慶",51:"四川",52:"貴州",53:"雲南",54:"西藏 ",61:"陝西",62:"甘肅",63:"青海",64:"寧夏",65:"新疆",71:"臺灣",81:"香港",82:"澳門",91:"國外 "};
var tip = "";
var pass= true;
if(!code || !/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[12])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/i.test(code)){
tip = "身份證號格式錯誤";
pass = false;
}
else if(!city[code.substr(0,2)]){
tip = "地址編碼錯誤";
pass = false;
}
else{
//18位身份證需要驗證最後一位校驗位
if(code.length == 18){
code = code.split('');
//∑(ai×Wi)(mod 11)
//加權因子
var factor = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 ];
//校驗位
var parity = [ 1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2 ];
var sum = 0;
var ai = 0;
var wi = 0;
for (var i = 0; i < 17; i++)
{
ai = code[i];
wi = factor[i];
sum += ai * wi;
}
var last = parity[sum % 11];
if(parity[sum % 11] != code[17]){
tip = "校驗位錯誤";
pass =false;
}
}
}
if(!pass) alert(tip);
return pass;
}
/**
* 頁面訪問設備識別
*/
function pcOrPhone(){
if(/AppleWebKit.*Mobile/i.test(navigator.userAgent) || (/MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alcatel|BIRD|DBTEL|Dopod|PHILIPS|HAIER|LENOVO|MOT-|Nokia|SonyEricsson|SIE-|Amoi|ZTE/.test(navigator.userAgent))){
try{
if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)){
//觸屏手機版地址
window.location.href="http://m.39yst.com/";
}else if(/iPad/i.test(navigator.userAgent)){
//pad版地址
window.location.href="http://pad.39yst.com/";
}else{
//普通手機版地址
window.location.href="http://m.39yst.com/";
}
}
catch(e){}
}
}
//日期字符串轉換成時間戳
function get_unix_time(dateStr){
var newstr = dateStr.replace(/-/g,'/');
var date = new Date(newstr);
var time_str = date.getTime().toString();
return time_str.substr(0, 10);
}
//時間戳生成日期格式
function getLocalTime(time){
if(time > 0){
var dateStr = new Date(time * 1000);
var str = "" + dateStr.getFullYear() + "-";
var m = dateStr.getMonth()+1;
m = m < 10 ? "0" + m : m;
str += m +"-";
var d = dateStr.getDate();
d = d < 10 ? "0" + d : d;
str += d;
return str;
}else{
return '末知時間';
}
}
//獲取最近12個月
function lastMonth(now) {
var mydate = new Date(now.replace(/[^\d]/g, "/") + "/1");
var result = [now];
for(var i = 0; i < 11; i++) {
mydate.setMonth(mydate.getMonth() - 1);
var m = mydate.getMonth() + 1;
m = m < 10 ? "0" + m : m;
result.push(mydate.getFullYear() + "/" + m +"月");
}
return result;
}
/**
*js實現全選,反選,取消選擇
*/
$(function () {
//全選或全不選--<input type="checkbox" id="all">
$("#all").click(function(){
if(this.checked){
$("#list :checkbox").prop("checked", true);
}else{
$("#list :checkbox").prop("checked", false);
}
});
//全選--<input type="button" value="全選" id="selectAll">
$("#selectAll").click(function () {
$("#list :checkbox,#all").attr("checked", true);
});
//全不選--<input type="button" value="全不選" id="unSelect">
$("#unSelect").click(function () {
$("#list :checkbox,#all").attr("checked", false);
});
//反選--<input type="button" value="反選" id="reverse">
$("#reverse").click(function () {
$("#list :checkbox").each(function () {
$(this).attr("checked", !$(this).attr("checked"));
});
allchk();
});
//設置全選複選框
$("#list :checkbox").click(function(){
allchk();
});
//獲取選中選項的值
$("#getValue").click(function(){
var valArr = new Array;
$("#list :checkbox[checked]").each(function(i){
valArr[i] = $(this).val();
});
var vals = valArr.join(',');
alert(vals);
});
});
function allchk(){
var chknum = $("#list :checkbox").size();//選項總個數
var selnum = $("#list :checkbox[checked]").size();//選項總個數
if(chknum==selnum){
$("#all").attr("checked",true);
}else{
$("#all").attr("checked",false);
}
}
/**
*倒計時
*/
var i = 10;
$(".time").click(function(){
$(".time").val("10秒後重新發送");
var timer = setInterval(function(){
if(i<0){
clearInterval(timer);
$('.time').css('color','#000');
$('.time').val('獲取驗證碼');
}else{
$('.time').val(i+'秒後重新發送');
$('.time').css('color','#999');
$('.time').attr("disabled",true);
}
i--;
},1000)
});
/**
*js生成指定位數的校驗碼
*@param n 生成驗證碼的位數
*/
var chars = ['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
function generateMixed(n) {
var res = "";
for(var i = 0; i < n ; i ++) {
var id = Math.ceil(Math.random()*61);
res += chars[id];
}
return res;
}
/**
*jquery 阻止表單提交
*/
$(document).ready(function(){
$("form").submit(function(e){
e.preventDefault();
alert("表單阻止提交了");
});
});
/**
* js正則驗證密碼
* 6-12位字符,數字和字母的混合
*/
var pattern=/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,12}$/;
/**
*js設置cookie
*@param name cookie名稱
*@param value cookie值
*@param hours cookie的有效期
*/
function setCookie(name, value, hours) {
hours = hours ? hours: 24;
var date = new Date();
date.setTime(date.getTime() + hours * 60 * 60 * 1000);
document.cookie = name + '=' + escape(value) + ';expires=' + date.toGMTString() + ';domain=.phptext.com';
}
/**
*js獲取cookie
*@param name cookie名稱
*/
function getCookie(name) {
var cookies = document.cookie;
var arr = cookies.match(new RegExp('(^| )' + name + '=([^;]*)(;|$)'));
if (arr != null) return unescape(arr[2]);
return "";
};
/**
*js刪除cookie,不能直接刪除
*設置cookie的時間過期
*@param name cookie的名稱
*/
function delCookie(name) {
var date = new Date();
date.setTime(date.getTime() - 1);
document.cookie = name + '=' + getCookie(name) + ';expires=' + date.toUTCString();
}
/**
*刪除cookie
*@param {[type]} name cookie的名字
*/
function removeCookie(name){
setCookie(name,1,-1);
}
/**
*獲取當前日期
*@return 當前的日期
*/
function getDate(){
var myDate = new Date();
var str = '' + myDate.getFullYear() + '-';
var m = myDate.getMonth()+1;
m = m < 10 ? '0'+m : m;
var d = myDate.getDate();
d = d < 10 ? '0'+d : d;
str += m+'-';
str += d;
return str;
}
/**
*獲取當前的日期函數
*傳入一個時間戳,如果不傳則爲當前時間
*注意:如果是uinx時間戳記得乘於1000, 比如php函數time()獲得的時間戳就要乘於1000
*@type String timestamp 要轉換的時間戳格式 1469504554276
*@returns {String} 日期格式: 2016-07-26 10:55:38
*/
function ge_time_format(timestamp=false){
if(timestamp){
var date = new Date(timestamp);
}else{
var date = new Date();
}
Y = date.getFullYear(),
m = date.getMonth()+1,
d = date.getDate(),
H = date.getHours(),
i = date.getMinutes(),
s = date.getSeconds();
m = m < 10 ? '0'+m : m;
d = d < 10 ? '0'+d : d;
H = H < 10 ? '0'+H : H;
i = i < 10 ? '0'+i : i;
s = s < 10 ? '0'+s : s;
var t = Y+'-'+m+'-'+d+' '+H+':'+i+':'+s;
return t;
}
/**
* 日期函數轉爲時間戳格式
* 傳入一個日期時間格式,如果不傳入就是獲取現在的時間了
* @type String strtime 要轉換的日期時間格式 2016-07-26 10:55:38
* @return {String} 時間戳格式: 1469504554276
*/
function get_unix_time_stamp(strtime=false){
if(strtime){
var date = new Date(strtime);
}else{
var date = new Date();
}
time1 = date.getTime(); //會精確到毫秒---長度爲13位
//time2 = date.valueOf(); //會精確到毫秒---長度爲13位
//time3 = Date.parse(date); //只能精確到秒,毫秒將用0來代替---長度爲10位
return time1;
}
/**
*信息提示
*點擊是彈出不透明的彈層
*@param str 提示說明的信息
*/
function showmsg(str){
jQuery("p[rel='warning_txt']").html(str);
jQuery(".tishi_box").show();
jQuery(".bg").show();
}
/**
* 異步加載,滾動時候加載
*/
function nextPage(){
if (pages == 0){
jQuery('.load_img').hide();
return;
}
currentPage++;
if(currentPage>pages){
jQuery('#no_more').show();
return;
}
if( jQuery('.load_img').is(":hidden")){
jQuery('#loading').show();
}
var response = $.ajax({
type: "POST",
url: "/index.php?m=content&c=venue&a=getList",
data: {
"name":name,
"type":type,
"page" : currentPage,
"pTime":new Date().getTime()
},
success: function(data){
jQuery('.fishing-list').append(data);
jQuery('#loading').hide();
jQuery('.load_img').hide();
},
async: true
});
}
/**
*實現滾動加載下一頁
*/
$(window).scroll(function(){
if (getScrollTop() + getClientHeight() >= getScrollHeight()){
nextPage();
}
});
/**
*搜索框
*搜索時過濾非法的不正常符號
*/
function csubmit(){
var val = $('#searchtext').val(),
kay = [',','.','\\','#','%','(',')'];
if ($.trim(val)==='') {
showmsg('請輸入關鍵字'); return false;
};
if ($.inArray(val, kay)>=0) {
showmsg('請輸入正確的關鍵字'); return false;
};
return true;
}
JS整理
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.