NiceIce
@NiceIce
lamer

Как лучше скоротить скрипт с .each() и if\else?

Здравствуйте, как скоротить данный скрипт ?
У самого не хватает знаний что бы уменшить его не потеряв функционал.
В приоритете уменьшить часть с var-ы : top,left,bottom,right.

var top = {
                trigger: "hover",
                container: "body",
                placement: "top",
                html: !0,
                viewport: {
                    selector: "body",
                    padding: 2
                },
                template: '<div class="popover"><div class="arrow"></div><h3 class="popover-title"></h3><div class="popover-content"></div></div>'
                },
                bottom = {
                trigger: "hover",
                container: "body",
                placement: "bottom",
                html: !0,
                viewport: {
                    selector: "body",
                    padding: 2
                },
                template: '<div class="popover"><div class="arrow"></div><h3 class="popover-title"></h3><div class="popover-content"></div></div>'
                },
                left = {
                trigger: "hover",
                container: "body",
                placement: "left",
                html: !0,
                viewport: {
                    selector: "body",
                    padding: 2
                },
                template: '<div class="popover"><div class="arrow"></div><h3 class="popover-title"></h3><div class="popover-content"></div></div>'
                },
                right = {
                trigger: "hover",
                container: "body",
                placement: "right",
                html: !0,
                viewport: {
                    selector: "body",
                    padding: 2
                },
                template: '<div class="popover"><div class="arrow"></div><h3 class="popover-title"></h3><div class="popover-content"></div></div>'
                };
                $('[data-toggle="popover"]').each(function() {
                    var width = $(window).width();
                    if(width<768){
                        if ($(this).hasClass("xs-bottom")){ $(this).popover(bottom);}
                        else if ($(this).hasClass("xs-top")){ $(this).popover(top);}
                        else if ($(this).hasClass("xs-left")){ $(this).popover(left);}
                        else if($(this).hasClass("xs-right")){ $(this).popover(right);}
                    }else if(width>=768 && width<992){
                        if ($(this).hasClass("sm-bottom")){ $(this).popover(bottom);}
                        else if ($(this).hasClass("sm-top")){ $(this).popover(top);}
                        else if ($(this).hasClass("sm-left")){ $(this).popover(left);}
                        else if($(this).hasClass("sm-right")){ $(this).popover(right);}
                    }else if(width>=992 && width<1200){
                        if ($(this).hasClass("md-bottom")){ $(this).popover(bottom);}
                        else if ($(this).hasClass("md-top")){ $(this).popover(top);}
                        else if ($(this).hasClass("md-left")){ $(this).popover(left);}
                        else if($(this).hasClass("md-right")){ $(this).popover(right);}
                    }else if(width>=1200 && width<1600){
                        if ($(this).hasClass("lg-bottom")){ $(this).popover(bottom);}
                        else if ($(this).hasClass("lg-top")){ $(this).popover(top);}
                        else if ($(this).hasClass("lg-left")){ $(this).popover(left);}
                        else if($(this).hasClass("lg-right")){ $(this).popover(right);}
                    }else if(width>=1600){
                        if ($(this).hasClass("xlg-bottom")){ $(this).popover(bottom);}
                        else if ($(this).hasClass("xlg-top")){ $(this).popover(top);}
                        else if ($(this).hasClass("xlg-left")){ $(this).popover(left);}
                        else if($(this).hasClass("xlg-right")){ $(this).popover(right);}
                    }
                });
  • Вопрос задан
  • 230 просмотров
Решения вопроса 2
werty1001
@werty1001
undefined
Какой раз вы уже задаете здесь этот вопрос? )
$(function() {

    var width = $(window).width(),
        size = width < 768 ? 'xs' : width < 992 ? 'sm' : width < 1200 ? 'md' : width > 1600 ? 'xlg' : 'lg',
        options = {
            html: !0,
            trigger: 'hover',
            container: 'body',
            viewport: {
                selector: 'body',
                padding: 2
            },
            template: '<div class="popover"><div class="arrow"></div><h3 class="popover-title"></h3><div class="popover-content"></div></div>'
        };

    $( '[data-toggle="popover"]' ).each( function() {
        
        var toggle = $(this);

            $.each([ 'bottom', 'top', 'left', 'right' ], function( i, value ) {

                var cls = size + '-' + value;

                if ( toggle.hasClass( cls ) ) options.placement = value;

                if ( cls === 'xs-top' && toggle.hasClass( 'error' ) ) {
                    options.delay = { show: 500, hide: 10 };
                    delete options.viewport;
                }

                toggle.popover( options );

            });

    });

});
Ответ написан
Rsa97
@Rsa97
Для правильного вопроса надо знать половину ответа
var re = /(?:^|\s)(?:xs|sm|md|lg|xlg)-(bottom|top|left|right)(?:$|\s)/;
$('[data-toggle="popover"]').each(function() {
  var classList = $(this).attr('class');
  var ok = re.exec(classList);
  if (ok !== null) {
    $(this).popover({
      trigger: "hover",
      container: "body",
      placement: ok[1],
      html: !0,
      viewport: {
        selector: "body",
        padding: 2
      },
      template: '<div class="popover"><div class="arrow"></div><h3 class="popover-title"></h3><div class="popover-content"></div></div>'
    });
  }
});
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы
01 мая 2024, в 10:55
2500 руб./за проект
01 мая 2024, в 10:55
3000 руб./за проект
01 мая 2024, в 10:55
3000 руб./за проект