var event = function (obj, eventType, callback) { obj.live(eventType, callback); }; var Verification = function () { this.isEmpty = function (objVal, msg) { if (!objVal) { this.alert("empty", msg); return false; } return objVal; }; this.alert = function (type, msg) { switch (type) { case "empty" : { alert(msg); break; } default : { alert("亲!请填写!"); } } } }; var ajax = function (url, callback) { $.ajax({ type: "get", async: false, url: url, dataType: "jsonp", jsonp: "callback", jsonpCallback: "flightHandler", success: callback, error: function () { } }); }; var Vote = function (type) { var _self = this; this.type = type; this.answer = []; this.obj = $('input[name="submit_vote"]'); this.M = function () { var options = function () { var options = []; for (var i = 0; i < $('.vote_options label').length; i++) { var option = $.trim($('.vote_options label').eq(i).text()); options.push(option); } return options; }; var sum_num = function (datas) { var sum = 0; for (var i = 0; i < datas['vote_result'].length; i++) { sum += parseFloat(datas['vote_result'][i]); } return sum; }; var result = function (data) { var option_items = options(); var sum = sum_num(data); var vote_res = []; for (var i = 0; i < data['vote_result'].length; i++) { var g = {}; g.name = option_items[i]; g.num = parseInt(data['vote_result'][i]); g.percentage = data['vote_result'][i] ? Math.round(data['vote_result'][i] / sum * 100) : 0; vote_res.push(g); } return vote_res; }; return { voteRes: function (datas, callback) { console.log(datas); var special_id = $('.vote').attr('data-special_id'); // var url = document.location.protocol+"//"+document.location.host+'/mobile/index.php?act=special&op=vote&client_type=ajax&special_id='+special_id+'&options='+datas; var url = 'http://192.168.0.200/mobile/index.php?act=special&op=vote&client_type=ajax&special_id=' + special_id + '&options=' + datas; ajax(url, function (data) { if (data.code !== 200) { alert(data.message); return; } var res = result(data.datas); callback(res); }); } } }; this.V = function (datas) { var resData = datas; var html = '
'; for (var i = 0; i < resData.length; i++) { html += '
' + '
' + resData[i].name + '
' + '
' + '
' + '
' + '
' + '
' + '' + resData[i].num + '票' + '' + resData[i].percentage + '%' + '
' + '
'; } html += '
'; return html; }; this.C = function () { var statShow = function () { if ($('.percentage').length > 0) { setTimeout(function () { for (var i = 0; i < $('.percentage').length; i++) { var num = $('.percentage')[i].innerHTML; $('.stat')[i].style.width = num; } }, 300); clearInterval(a); } }; var a = setInterval(statShow, 10); event(_self.obj, 'change', function () { switch (_self.type) { case 0: case 1 : { _self.answer = []; $.each(_self.obj, function () { if (this.checked) { _self.answer.push($(this).val()); } }); break; } case 2: { var val = $(this).val(); if ($(this).prop('checked')) { if (_self.answer.length >= 2) { for (var g = 0; g < _self.obj.length; g++) { if (_self.obj.eq(g).val() == _self.answer[0]) { _self.obj.eq(g).prop('checked', false); _self.answer.splice(0, 1); _self.answer.push(val); break; } } } else { _self.answer = []; $.each(_self.obj, function () { if (this.checked) { _self.answer.push($(this).val()); } }); } } else { _self.answer = []; $.each(_self.obj, function () { if (this.checked) { _self.answer.push($(this).val()); } }); } } } }); event($('#submit_btn'), 'click', function () { if ($(this).hasClass('button_null')) { return; } if (_self.answer.length == '') { alert('亲,请投票!'); return false; } _self.M().voteRes(_self.answer, function (res) { $('.vote_pro').after(_self.V(res)); }); $('.vote_options').css('display', 'none'); $(this).addClass('button_null').html('已完成投票'); statShow(); }) }; }; var Question = function (obj, handleType) { this.obj = obj; this.handleType = handleType; this.answer = []; this.getAnswer = function () { return this.answer; }; this.C = function () { var _self = this; _self.obj.on('change', function () { switch (_self.handleType) { case 0: case 1 : { _self.answer = []; $.each(_self.obj, function () { if (this.checked) { _self.answer.push($(this).val()); } }); break; } case 2: { var val = $(this).val(); if ($(this).prop('checked')) { if (_self.answer.length >= 2) { for (var g = 0; g < _self.obj.length; g++) { if (obj.eq(g).val() == _self.answer[0]) { obj.eq(g).prop('checked', false); _self.answer.splice(0, 1); _self.answer.push(val); break; } } } else { _self.answer = []; $.each(_self.obj, function () { if (this.checked) { _self.answer.push($(this).val()); } }); } } else { _self.answer = []; $.each(_self.obj, function () { if (this.checked) { _self.answer.push($(this).val()); } }); } break; } } }); } }; var QuestionList = function () { var question_items = []; (function (question_items) { var questions = $('.question').length; var num = 1; var question_nums = 'q' + num; for (var j = 0; j < questions; j++) { question_nums = new Question($('input[name="question' + num + '"]'), parseInt($('.question_type').eq(j).attr('data-type'))); question_items.push(question_nums); question_nums.C(); num++; } })(question_items); var answers = function () { var answers = {}; answers.type = 'question'; answers['answers'] = []; for (var i = 0; i < question_items.length; i++) { answers['answers'].push(question_items[i].getAnswer()); } return answers; }; this.submit = function () { $('#submit_question').on('click', function () { var resAnswers = answers(); console.log(resAnswers); for(var i = 0;i < resAnswers['answers'].length;i++) { if(resAnswers['answers'][i].length == 0) { var noQuestion = $('input[name="question'+(i+1)+'"]').offset().top; window.scrollTo(0,noQuestion-120); alert('请回答第'+(i+1)+'题'); return; } } }) }; }; var CommentsBox = function () { this.V = function () { var html = '
' + '' + '' + '
' + '
' + '
取消
' + '
发送
' + '
' + '
' + '' + '
' + '
' + '

剩余250

' + '
' + '
' + '
'; return html; }; this.M = function () { }; this.C = function () { var _self = this; var maxSize = 250; return { appendHtml: function () { $('body').append(_self.V()); }, show: function () { event($('.msg_btn'), 'touchend', function () { $(this).removeClass('active'); $('.msg_icon_null').removeClass('msg_icon'); $('.pop').show(); $('.answer_comments').show(); $('#comments').focus(); $("body").on("touchmove", function (event) { event.preventDefault; }, false) }); }, close: function () { event($('.close'), 'click', function () { $('#comments').val(''); $('.surplus').html(maxSize); $('.pop').hide(); $('.answer_comments').hide(); $("body").off("touchmove"); }) }, surplusInput: function () { event($('#comments'), 'input', function () { var val = $(this).val().length; var surplus = 0; if (val <= maxSize) { surplus = maxSize - val; } else { $(this).val($(this).val().substring(0, maxSize)); } $('.surplus').html(surplus); }) }, submit: function () { event($('.submit'), "click", function () { var verification = new Verification(); verification.isEmpty($('#comments').val(), '亲,内容不能为空哦'); $('.pop').css('display', 'none'); $('#comments').val(''); $("body").off("touchmove"); }) } } } }; var PageVideo = function(){ var createDom = function(videosrc){ var a = document.createElement('video'); a.poster = './images/image01.png'; a.src = videosrc; return a; }; this.append = function(){ for(var i = 0;i<$('.video_box').length;i++) { var videosrc = $('.video_box').eq(i).attr('data-src'); $('.video_box').eq(i).append(createDom(videosrc)); } }; }; var Pagevideo = new PageVideo(); Pagevideo.append(); var vote = new Vote(parseInt($('.vote_type').attr('data-type'))); vote.C(); var questionList = new QuestionList(); questionList.submit(); var commentsBox = new CommentsBox(); for (var i in commentsBox.C()) { commentsBox.C()[i](); } event($('.like_btn'), 'click', function () { $(this).toggleClass('active'); $(this).find('.like_icon_null').toggleClass('like_icon'); }); event($('.close_icon'), 'click', function () { $('.pop').hide(); $('.bonus_page').hide(); $('.close_icon').hide(); }); event($('.comment_label'), 'click', function () { $('.pop').show(); $('.bonus_page').addClass('fade-in').show(); $('.close_icon').addClass('rotate').show(); });