Редактор темы

Вопрос? Проблема с рейтингом при ajax загрузке новостей

  • Автор темы Автор темы Burial
  • Дата начала Дата начала

Burial

Местный
Регистрация
30 Сен 2015
Сообщения
2.015
Рейтинг реакций
231
Баллы
63
Возраст
43
1/3
Автор темы
Проблема с правильным отображением рейтинга, при использовании Ajax подгрузка новостей.
Если использовать обычную, постраничную навигацию, то рейтинг отображается верно 100%. Если использовать Ajax подгрузку, то рейтинг ломается на +2175 и т.д. Помню, что надо править libs.js, но уже забыл решение. Кто шарит в скриптах, подскажите решение! )
Примеры До и После
Screenshot_6.jpg
Screenshot_1.jpg

Код shorstory:
Код:
                <div class="thumb-item__label thumb-item__label--rating">
                    <span class="myicon-thumb-up6"></span><span class="js-count-percent">{rating}{vote-num}</span>
                </div>
Код libs.js
Код:
$(document).ready(function(){
   
    /* ====================== BUILDING ====================== */

    $('.js-count-percent').each(function() {
        var a = $(this), b = parseInt(a.find('.ratingtypeplusminus').text(),10), c = parseInt(a.find('span[id*=vote]').text(),10);
        if ( c >= b && c > 0 ) {
            var t = Math.round((c - (c - b)/2)/c*100);
            a.text(t+'%').addClass('high');
            if ( t < 50 ) { a.addClass('low'); }
        } else { a.text('0%'); };
        a.addClass('wdone');
    });
   
    $('.inner-page__rating').each(function(){
        var rate = $(this),
            p = parseInt(rate.find('.inner-page__rating-likes').text(), 10), m = parseInt(rate.find('.inner-page__rating-dislikes').text(), 10),
            perc = p+m, fill = Math.round(p/perc*100);
            rate.prepend('<div class="inner-page__rating-percent">0%</div><div class="inner-page__rating-bar"><div class="inner-page__rating-fill"></div></div>');
        if ( p > 0 || m > 0 ) {
            rate.find('.inner-page__rating-fill').css({'width':''+fill+'%'});
            rate.find('.inner-page__rating-percent').html(''+fill+'%');
        };
        console.log('лайк '+p+' дизлайк '+m+' общее '+perc+' процент '+fill+'');
    });  
   
    $('body').append('<div class="overlay hidden"></div><div class="mobile-menu" id="mobile-menu"></div><div class="mobile-menu-close"><span class="far fa-times"></span></div><div id="scrolltop"><span class="far fa-long-arrow-up"></span></div>');
    $('.js-this-in-mobile-menu').each(function() {
        $(this).clone().appendTo('#mobile-menu');
    });  
   
    /* ====================== EVENTS ====================== */

    $(document).on('click','.js-show-login',function(){
        $('.overlay, .login').fadeIn(200);
        $('body').addClass('modal-is-opened');
        return false;
    });  
    $(".js-show-mobile-menu").click(function(){
        $('.overlay').fadeIn(200);
        $('#mobile-menu, .mobile-menu-close').addClass('is-active');
        $('body').addClass('mobile-menu-is-opened');
    });
    $(document).on('click','.overlay, .login__close, .mobile-menu-close',function(){
        $('.overlay, .login').fadeOut(200);
        $('#mobile-menu, .mobile-menu-close').removeClass('is-active');
        $('body').removeClass('modal-is-opened mobile-menu-is-opened');
    });
   
    $(document).on({
        mouseenter: function () {
            var mimg = $(this), mimgsrc = mimg.data('modelimg'), mimgtitle = mimg.text(),
            winHeight = window.innerHeight, mimgPosTop = mimg.offset().top - $(window).scrollTop();
            mimg.append('<div class="model-preview img-wide"><img src="'+mimgsrc+'" alt="'+mimgtitle+'"></div>');
            if (mimgPosTop < winHeight/2) { mimg.addClass('bottom'); };
        },
        mouseleave: function () {
            $('.model-preview').remove();
            $('[data-modelimg]').removeClass('bottom');
        }
    },'[data-modelimg]');
   
    /* ====================== DLE SCRIPTS ====================== */

    $(document).on('click','.inner-page__comments-btn',function(){
        $('.add-comments-form').slideToggle(200);
    });
    $(document).on('click','.form__textarea-inside textarea, .fr-wrapper',function(){
        $('.add-comments-form').find('.form__row--protect').removeClass('hidden');
    });
    $('#dle-content > #dle-ajax-comments').appendTo($('#inner-page__comments'));
    $('.login__social-btns a').on('click',function(){
           var href = $(this).attr('href'), width  = 820, height = 420,
               left   = (screen.width  - width)/2, top   = (screen.height - height)/2-100;  
        auth_window = window.open(href, 'auth_window', "width="+width+",height="+height+",top="+top+",left="+left+"menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no");
           return false;
    });
    $('.js-comm-author').each(function(){
        var a = $(this), b = a.closest('.js-comm'), c = a.text().substr(0,1),
            f = b.find('.js-comm-avatar'), e = f.children('img').attr('src'),
            d = ["#c57c3b","#753bc5","#79c53b","#eb3b5a","#45aaf2","#2bcbba","#778ca3"], rand = Math.floor(Math.random() * d.length);
        if (e == '/templates/'+dle_skin+'/dleimages/noavatar.png') {
            f.html('<div class="comment-item__letter d-flex jc-center ai-center" style="background-color:'+d[rand]+'">'+c+'</div>');
        };
    });  
    var gotop = $('#scrolltop');
    $(window).scroll (function () {
        if ( $(this).scrollTop () > 300 ) { gotop.fadeIn(200); }
        else { gotop.fadeOut(200); }
    });  
    gotop.click(function(){
        $('html, body').animate({ scrollTop : 0 }, 'slow');
    });

});

function doRateLD( rate, id ) {
    ShowLoading('');
    $.get(dle_root + "engine/ajax/controller.php?mod=rating", { go_rate: rate, news_id: id, skin: dle_skin, user_hash: dle_login_hash }, function(data){
        HideLoading('');
        if ( data.success ) {
            var rating = data.rating;
            rating = rating.replace(/&lt;/g, "<");
            rating = rating.replace(/&gt;/g, ">");
            rating = rating.replace(/&amp;/g, "&");
            $("#ratig-layer-" + id).html(rating);
            $("#vote-num-id-" + id).html(data.votenum);
            var rt = parseInt($(rating).text()),
                m = (data.votenum - rt)/2,
                p = data.votenum - m,
                fill = Math.round(p/data.votenum*100),
                frate = $('#inner-page__rating--' + id);
            frate.find('.inner-page__rating-percent').html(''+fill+'%');
            frate.find('.inner-page__rating-fill').css({'width':''+fill+'%'});
        } else if (data.error) {DLEalert ( data.errorinfo, dle_info );}
    }, "json");
};


/* END */
$(document).ready(function(){
   
         $('body').on('click','#nav-load a',function(){
        var urlNext = $(this).attr('href');
        var scrollNext = $(this).offset().top - 200;
        if (urlNext !== undefined) {
            $.ajax({
                url: urlNext,
                beforeSend: function() {
                    ShowLoading('');
                },          
                success: function(data) {
                    $('#bottom-nav').remove();
                    $('#dle-content').append($('#dle-content', data).html());
                    $('#dle-content').after($('#bottom-nav'));
                    window.history.pushState("", "", urlNext);
                    $('html, body').animate({scrollTop:scrollNext}, 800);  
                    HideLoading('');
                },
                  error: function() {              
                    HideLoading('');
                    alert('что-то пошло не так');
                  }
            });
        };
        return false;
    });
   
});
 
После
Код:
$('#dle-content').append($('#dle-content', data).html());
Вставить
Код:
$('.js-count-percent').each(function() {
    var a = $(this), b = parseInt(a.find('.ratingtypeplusminus').text(),10), c = parseInt(a.find('span[id*=vote]').text(),10);
    if ( c >= b && c > 0 ) {
        var t = Math.round((c - (c - b)/2)/c*100);
        a.text(t+'%').addClass('high');
        if ( t < 50 ) { a.addClass('low'); }
    } else { a.text('0%'); };
    a.addClass('wdone');
});

$('.inner-page__rating').each(function(){
    var rate = $(this),
        p = parseInt(rate.find('.inner-page__rating-likes').text(), 10), m = parseInt(rate.find('.inner-page__rating-dislikes').text(), 10),
        perc = p+m, fill = Math.round(p/perc*100);
        rate.prepend('<div class="inner-page__rating-percent">0%</div><div class="inner-page__rating-bar"><div class="inner-page__rating-fill"></div></div>');
    if ( p > 0 || m > 0 ) {
        rate.find('.inner-page__rating-fill').css({'width':''+fill+'%'});
        rate.find('.inner-page__rating-percent').html(''+fill+'%');
    };
    console.log('лайк '+p+' дизлайк '+m+' общее '+perc+' процент '+fill+'');
});
 
Автор темы
PunPun Спасибо мастер!
 

Похожие темы Последние темы Популярные темы

Назад
Сверху