123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419 |
- 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 = '<div class="results">';
- for (var i = 0; i < resData.length; i++) {
- html += '<div class="result">' +
- '<div class="result_option">' + resData[i].name + '</div>' +
- '<div class="status">' +
- '<div class="status_line">' +
- '<div class="stat"></div>' +
- '<div class="status_bg"></div>' +
- '</div>' +
- '<span class="num">' + resData[i].num + '票</span>' +
- '<span class="percentage">' + resData[i].percentage + '%</span>' +
- '</div>' +
- '</div>';
- }
- html += '</div>';
- 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 = '<div class="pop">' +
- '<a href="javascript:void(0)" class="bonus_page"></a>' +
- '<a href="javascript:void(0)" class="close_icon"></a>' +
- '<div class="answer_comments">' +
- '<div class="handle">' +
- '<div><a href="javascript:void(0)" class="close">取消</a></div>' +
- '<div><a href="javascript:void(0)" class="submit">发送</a></div>' +
- '</div>' +
- '<div class="comments_box">' +
- '<textarea name="" id="comments" cols="30" rows="10" class="comments"></textarea>' +
- '</div>' +
- '<div class="msg_num">' +
- '<p>剩余<span class="surplus">250</span>字</p>' +
- '</div>' +
- '</div>' +
- '</div>';
- 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();
- });
|