简体   繁体   English

如何调整twitter bootstrap popover的大小?

[英]How to resize twitter bootstrap popover?

I am using bootstrap in my mvc project. 我在我的mvc项目中使用bootstrap。 I have an issue with bootstrap popover widget. 我有一个bootstrap popover小部件的问题。 I have created a custom knockout binding for popover widget, here the code : 我为popover小部件创建了一个自定义的敲除绑定,这里是代码:

Check fiddle 检查小提琴

 ko.bindingHandlers.popover = {
        init: function (element, valuesAccessor, allBindingsAccessor, viewModel, bindingContext) {
            var options = ko.utils.unwrapObservable(valuesAccessor() || {});

            options.html = true;

            options.content = '<small class="text-info">' + 'Variable text goes here.Variable text goes here.Variable text goes here.Variable text goes here.Variable text goes here. ' + '</small>';

            $(element).popover(options);
        },
        update: function (element, valuesAccessor, allBindingsAccessor, viewModel, bindingContext) {
            var extraOptions = allBindingsAccessor().popoverOptions;

            $(element).popover(extraOptions.observable() ? "show" : "hide");
            $(element).siblings('.popover').css({ width: '150px' });

            //IF YOU UN-COMMENT BELOW 2 LINES THAN EVERY THINGS WORKS FINE

            //if(extraOptions.observable())
                //$(element).popover('show');
        }
    };

    function vm() {
        var self = this;

        self.isVisible = ko.observable(false);

        self.open = function () {
            self.isVisible(!self.isVisible());
        };
    }

    ko.applyBindings(new vm());

I want to initialize popover on any element with variable text message and with variable size. 我想在任何带有可变文本消息和可变大小的元素上初始化popover。 Every thing goes ok but when i change the default width of the popover than on first time when it open its position is not correct (please check the behavior in fiddle) . 每件事情都没问题,但是当我改变弹出窗口的默认宽度时,它比第一次打开它的位置是不正确的(请检查小提琴中的行为)

There was some line of code in the fiddle which if you uncomment than this issue solve. 小提琴中有一些代码,如果你取消注释,这个问题就解决了。 But i feel it is a hacky approach, i want some better way to handle if there is any ? 但我觉得这是一种hacky方法,我想要一些更好的方法来处理,如果有的话?

Here's a sample initialization that I use. 这是我使用的示例初始化。

    $(".property-price")
    .popover({ 
            trigger: "hover", 
            placement: 'bottom',
            toggle : "popover",
            content : "The from price is calculated ba....the dates selected.",
            title: "How is the from price calculated?",
            container: 'body',
            template: '<div class="popover popover-medium"><div class="arrow"></div><div class="popover-inner"><h3 class="popover-title"></h3><div class="popover-content"><p></p></div></div></div>',
        });

As you can see, it uses a custom template. 如您所见,它使用自定义模板。 The template uses a custom popover-medium class. 该模板使用自定义popover-medium类。

I then have a CSS selector 然后我有一个CSS选择器

.popover-medium {
    max-width: 350px;
}

You could also just set a style on the template class if you wanted. 如果需要,您也可以在模板类上设置样式。

DEMO DEMO

Try this I have edited your code 试试这个我编辑了你的代码

ko.bindingHandlers.popover = {
        init: function (element, valuesAccessor, allBindingsAccessor, viewModel, bindingContext) {
            var options = ko.utils.unwrapObservable(valuesAccessor() || {});



            options.html = true;

            options.content = '<small class="text-info">' + 'Variable text goes here.Variable text goes here.Variable text goes here.Variable text goes here.Variable text goes here. ' + '</small>';

            $(element).popover(options);


        },
        update: function (element, valuesAccessor, allBindingsAccessor, viewModel, bindingContext) {
            var extraOptions = allBindingsAccessor().popoverOptions;

            $(element).popover(extraOptions.observable() ? "show" : "hide");


            //IF YOU UN-COMMENT BELOW 2 LINES THAN EVERY THINGS WORKS FINE

            //if(extraOptions.observable())
                //$(element).popover('show');
        }
    };

    function vm() {
        var self = this;

        self.isVisible = ko.observable(false);

        self.open = function () {
            self.isVisible(!self.isVisible());
        };
    }

    ko.applyBindings(new vm());

just add this css 只需添加此CSS

.popover {
    max-width: 150px;
    width: auto;
}

Hope this helps Thank you 希望这有帮助谢谢

You're changing the width after initializing the popover. 初始化弹出框后,您正在更改宽度。 The calculated top left position of the popover stays in place, but the width gets smaller. 弹出的计算左上角位置保持不变,但宽度变小。 This causes the height to get greater. 这导致高度变大。

You'll need to reorder your events so that width is applied before popover is initialized. 您需要重新排序事件,以便在初始化弹出窗口之前应用宽度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM