$(document).ready(function () { $.plugin = { number_plus: function (obj, set_num, speed) { if (!speed) speed = 60; if (!set_num) set_num = 50; var scroll_lock = true; $(window).scroll(function () { if ($(document).scrolltop() + $(window).height() > obj.offset().top && obj.offset().top + obj.innerheight() > $(document).scrolltop() && scroll_lock) { scroll_lock = false; var time_lock = 0, this = obj.find('span'), // 获取对象 num = []; // 数据容器 var set_plus = setinterval(function () { // 数字相加 this.each(function (index) { if (!num[index]) num[index] = $(this).find('em').attr('data-num')*1/set_num; else num[index] += $(this).find('em').attr('data-num')*1/set_num; $(this).find('em').text(parseint(num[index])); if (num[index] > $(this).find('em').attr('data-num')*1) { $(this).find('em').text($(this).find('em').attr('data-num')*1); time_lock++; } }); if (time_lock >= num.length) { // 清理多余执行 clearinterval(set_plus); } }, speed); } }) }, bg_parallax: function (obj, speed) { if (!speed) speed = .5; $(window).scroll(function () { obj.css({ 'background-position-y': (((obj.offset().top + obj.innerheight() / 2) - ($(document).scrolltop() + $(window).height() / 2)) * speed) }) }) }, interval_fun: function (obj, means, cycle, position) { if (!position) position = 2; if (!cycle) var cycle_num = 0; $(window).scroll(function () { if (obj.offset().top + obj.innerheight() / position < $(document).scrolltop() + $(window).height() && obj.offset().top + obj.innerheight() / position > $(document).scrolltop()) { cycle_num++; if (!cycle && cycle_num <= 1) means(); else if (cycle) means() } }) }, video_open: function (obj) { var src, btn, img, tag, method = obj.attr('data-mode'); if (obj.attr('data-src')) src = obj.attr('data-src'); else return; if (obj.attr('data-img')) img = obj.attr('data-img'); btn = obj.attr('data-btn') ? obj.attr('data-btn') : '#000'; if (!method) tag = ''; else tag = ''; var video = "
" + tag + "
", css = "" obj.click(function () { $("body").append(video).append(css).animate(200, function () { $(".video_box").add($(".video_mask")).removeclass('active'); }); $("i.video_mask").add($("i.video_close")).one('click', function () { $(".video_box").add($("i.video_mask")).add($("#video_css")).remove(); }) }); }, pop_ups: function(obj,model){ var css = '', html = '', lock = true, model_form = false, model_url = ''; $("body").append(css).append(html); $.getscript('/static/js/layer/layer.js',function (response,status) { if (status==='success') { lock = false; } }); obj.on('click',function (e) { if (model && !model_form) { e.preventdefault(); e.stoppropagation(); model_url = $(this).attr('href')?$(this).attr('href'):$(this).find('a').attr('href'); } if (!model_form) $("#pups_from").stop().fadetoggle(200); }); $("#pups_from i.close").on('click',function () { $("#pups_from").stop().fadetoggle(200); }); $("#pups_from form input[type=submit]").on('click',function () { layer.load(0,{shade:0.1,shadeclose:false}); $.ajax({ type: 'post', url: '/api/contact/submit/uid/1.html', data: $("#pups_from form").serialize(), datatype: 'json', success: function (data) { layer.closeall(); if (data.status == 200) { layer.msg(data.result,{icon:1,time:4000}); if (model && !model_form) { model_form = true; $("#pups_from").stop().fadetoggle(200); var a = document.createelement('a'); a.setattribute('href',model_url); a.setattribute('download',''); document.body.appendchild(a); a.click(); a.remove(); } } else { layer.msg(data.result,{icon:2,time:4000}) } } }) }); }, line_curve: function (obj, origin, coordinate, color,img, speed) { if (!speed) speed = 0.01; if (!color) color = '#000'; var con = document.getelementbyid(obj).appendchild(document.createelement('canvas')); var ctx = con.getcontext('2d'); con.width = document.getelementbyid(obj).offsetwidth ? document.getelementbyid(obj).offsetwidth : window.innerwidth; con.height = document.getelementbyid(obj).offsetheight ? document.getelementbyid(obj).offsetheight : window.innerheight; var percent = 0; ctx.linewidth = 2; origin = [origin[0] * (con.width / 100), origin[1] * (con.height / 100)]; function animate() { ctx.clearrect(0, 0, con.width, con.height); ctx.drawimage(img,0,0,con.width,con.height); for (var i = 0; i < coordinate.length; i++) { var x0 = coordinate[i][0][0] * (con.width / 100); var y0 = coordinate[i][0][1] * (con.height / 100); var x1 = coordinate[i][1][0] * (con.width / 100); var y1 = coordinate[i][1][1] * (con.height / 100); ctx.strokestyle = color; ctx.beginpath(); formula(ctx, origin, [x0, y0], [x1, y1], percent); ctx.stroke(); } if (percent < 1) percent += speed; else { return; } requestanimationframe(animate); } function formula(ctx, start, end, control, percent) { // 二次贝塞尔曲线坐标计算公式 var v01 = [control[0] - start[0], control[1] - start[1]]; // 向量 var v12 = [end[0] - control[0], end[1] - control[1]]; // 向量 var q0 = [start[0] + v01[0] * percent, start[1] + v01[1] * percent]; var q1 = [control[0] + v12[0] * percent, control[1] + v12[1] * percent]; var v = [q1[0] - q0[0], q1[1] - q0[1]]; // 向量 var b = [q0[0] + v[0] * percent, q0[1] + v[1] * percent]; ctx.moveto(start[0], start[1]); ctx.quadraticcurveto( q0[0], q0[1], b[0], b[1] ); } animate(); }, shopping_cart: function (text_, element, cart_page,product) { // 对象数据汇总 if (!cart_page) cart_page = false; if (!element) { element = { shopping_cart: '.shopping_cart', shopping_form: '.shopping_form', shopping_list: '.shopping_list', product_box: '.product_box', //多增加一个data-id 属性 product_name: '.product_name', product_link: '.product_link', product_det: '.product_det', product_img: '.product_img', product_tag: '.product_tag', product_add: '.product_add' }; } if (!text_) { text_ = { close: 'close all', title: 'feedback', submit: 'send', number: 'number:', cart: 'there are no products, just add them!', required: 'can not be empty!' } } var css = '', html = ''; // 初始化 var data = [], lock = true; if ($.cookie !== undefined) { if ($.cookie('the_product_info') !== undefined && $.cookie('the_product_info') !== '') { data = $.cookie('the_product_info').split('?'); } } else { alert('需要cookie插件支持!'); return; } $(element.shopping_cart).find('em').html(data.length); // 列表 function click_car(t) { lock = true; var name = t.parents(element.product_box).find(element.product_name).length ? t.parents(element.product_box).find(element.product_name).text() : '', url = t.parents(element.product_box).find(element.product_link).length ? t.parents(element.product_box).find(element.product_link).attr('href') : '', det = t.parents(element.product_box).find(element.product_det).length ? t.parents(element.product_box).find(element.product_det).text() : '', img = t.parents(element.product_box).find(element.product_img).length ? t.parents(element.product_box).find(element.product_img).attr('src') : '', tag = t.parents(element.product_box).find(element.product_tag).length ? t.parents(element.product_box).find(element.product_tag).children('*') : ''; if ($.isarray(data)) { data.map(function (value, index) { var key = object.keys(json.parse(value))[0]; if (key === t.parents(element.product_box).attr('data-id')) { lock = false; data.splice(index, 1); } }); } var tag_arr = []; if (tag !== '') { tag.filter(function () { tag_arr.push($(this).text()) }); } var text = '{"' + t.parents(element.product_box).attr('data-id') + '":{"name":"' + name.replace(/\s/g, " ") + '","url":"' + url + '","det":"' + det + '","img":"' + img + '","tag":"' + tag_arr.join('/') + '"}}'; if (lock) { data.push(text); } $(element.shopping_cart).find('em').html(data.length); $.cookie('the_product_info', data.join('?'), {path: '/'}); } $(document).on('click', element.product_add, function (e) { click_car($(this)); $(this).toggleclass('active'); }); function cart_detect() { data.map(function (value, index) { var detect = json.parse(value); $(element.product_box).filter(function () { if (object.keys(detect)[0] === $(this).attr('data-id')) { $(this).find(element.product_add).addclass('active') } }) }); } //购物车 function structure_injection() { if (cart_page) { return data; }else { var data_ = []; $(element.shopping_list).html(''); data.map(function (value, index) { data_ = json.parse(value); $(element.shopping_list).append('
  • \n' + '
    \n' + '
    \n' + '
    \n' + '

    ' + data_[object.keys(data_)[0]].name + '

    ' + text_.number + '

    \n' + ' \n' + ' \n' + ' \n' + '
    \n' + '
    \n' + '
  • ') }); } } function cart() { if (!cart_page) { $('body').append(html,css); $.getscript('/static/js/layer/layer.js'); structure_injection(); $(document).on('click','#cart i.del', function (e) { var info_ = $(this).parents('li'); if (data.length) { data.splice(info_.index(), 1); $(element.shopping_cart).find('em').html(data.length); $.cookie('the_product_info', data.join('?'), {path: '/'}); info_.remove(); if (!data.length) { $(element.shopping_list).html(text_.cart); } } }); $(document).on('blur','#cart input', function (e) { if ($(this).val() === '') { layer.msg(text_.required, {icon: 2, time: 4000}); } }) $(document).on('click','#cart input[type=\'submit\']', function (e) { e.preventdefault(); var content = ''; $(element.shopping_list).find('li').filter(function () { content += '\n' + $(this).find('h4').text() + '\n' + 'qty:' + $(this).find('input').val() + '\n' + 'message:' + $(this).find('textarea').val() + '\n' + 'url:' + $(this).find('a').attr('href') + '\n' + '-------------------------------------------\n' }); var textarea = $(element.shopping_form).find("textarea").val(); $(element.shopping_form).find("textarea").val(textarea + content); layer.load(0, {shade: 0.1, shadeclose: false}); $.ajax({ type: 'post', url: '/api/contact/submit/uid/1.html', data: $(element.shopping_form).find('form').serialize(), datatype: 'json', success: function (ret) { layer.closeall(); $(element.shopping_form).find("textarea").val(textarea); if (ret.status == 200) { layer.msg(ret.result, {icon: 1, time: 4000}); $(element.shopping_cart).find('em').html(0); $.cookie('the_product_info', '', {path: '/'}); } else { layer.msg(ret.result, {icon: 2, time: 4000}) } } }) }); $(document).on('click','#cart span.close_all', function (e) { $(element.shopping_list).find('li').remove(); $(element.shopping_cart).find('em').html(0); $.cookie('the_product_info', '', {path: '/'}); }); $(document).on('click',element.shopping_cart, function (e) { $("#cart").fadein(300); structure_injection(); }) $(document).on('click','#cart i.fa-close', function (e) { $("#cart").fadeout(300) }) }else { product(structure_injection()) } } cart_detect(); cart(); } }; /** * 全局效果说明 * ---------------------dividing line-------------------- * @num_plus 数字相加 * 参数 (obj,set_num,speed = 60) * 详解 obj * 获取容器对象 * set_num * 设置相加比例 公式: 数字 / set_num = 每次相加数字(整数) * speed * 动画速度 默认 60ms * html 结构 *
      *
    • *0*
    • *
    * * ---------------------dividing line-------------------- * @bg_parallax 背景视差 * 参数 (obj,speed = 0.5) * 详解 obj * 获取容器对象 * speed * 动画速度 默认 0.5 * * ---------------------dividing line-------------------- * @interval_fun 区间判断 * 参数 (obj,means,cycle,position) * 详解 obj * 获取容器对象 * means * 执行方法 * cycle * 是否多次执行 boolean值 * position * 当容器自身多少出现在屏幕中时执行 公式:容器高度 / position * * ---------------------dividing line-------------------- * @video_open 视频弹窗 * 参数 (obj,url,btn,img) * 详解 obj * 获取点击对象 ( html标签 data-src="视频地址" data-img="video第一帧" data-btn="关闭按钮颜色") * method * 切换模式 (video / iframe) false / true * * ---------------------dividing line-------------------- * @line_curve 画曲线 * 参数 (obj,origin,coordinate,color,speed) * 详解 obj * 获取id属性 * origin * 中心点 (数组) 百分比 * coordinate * 其它点 (控制点+终点) * [ * [[终点],[控制点]] 百分比 * ] * color * 线条颜色 * img * 图片 * speed * 速度 * @shopping_cart 伪购物车 * 参数 (text_, element, cart_page = false) * 详解 text_ = { close: 'close all', title: 'feedback', submit: 'send', number: 'number:', cart: 'there are no products, just add them!', required: 'can not be empty!' } cart_page 是否启用自定义购物弹窗 product 不启用时 返回数据 注意 若提交失败 检查是否存在对应字段 * */ })