简体   繁体   English

Bootstrap工具提示动态CSS规范

[英]Bootstrap tooltip dynamic css specification

Bootstrap default tooltip template is black box and white text. Bootstrap的默认工具提示模板是黑盒和白色文本。 I can change .tooltip css class like this: (White background.) 我可以这样更改.tooltip CSS类:(白色背景。)

.tooltip{position:absolute;display:none;color:#333;text-align:left;font-size:0.9em;width:250px;}
.tooltip.in{opacity:0.9;z-index:1030;height:auto;display:block}
.tooltip.top{margin-top:-10px;text-align:center}
.tooltip.top{margin-top:-10px;text-align:center}
.tooltip.right{margin-left:10px}
.tooltip.bottom{margin-top:10px;text-align:center}
.tooltip.left{margin-left:-10px;text-align:right; position: absolute}
.tooltip-inner{display:inline-block;padding:10px;color:#666;background-color:white}
.tooltip-arrow{position:absolute;width:10px;height:0;}
.tooltip.top .tooltip-arrow{bottom:-5px;left:50%;margin-left:-5px;border-top-color:white;border-width:5px 5px 0}
.tooltip.right .tooltip-arrow{top:50%;left:-5px;margin-top:-5px;border-right-color:white;border-width:5px 5px 5px 0}
.tooltip.left .tooltip-arrow{top:50%;right:-5px;margin-top:-5px;border-left-color:white;border-width:5px 0 5px 5px}
.tooltip.bottom .tooltip-arrow{top:-5px;left:50%;margin-left:-5px;border-bottom-color:white;border-width:0 5px 5px}

I should change template of tooltip dynamically in angular directive. 我应该在角度指令中动态更改工具提示模板。

angular.module("app", []).directive("tooltip", function(){
    return {
        restrict: "A",
        scope: {
            title: "@",
            template: "@"
        },
        link: function(scope, element){
            $(element).tooltip({
                title: scope.title,
                placement: "right",
            });            
        }
    }
})

For example red-tooltip, blue-tooltip, .... 例如,红色工具提示,蓝色工具提示...。

<div ng-app="app">
    <a href="#" tooltip title="Title-1" template="red-tooltip">Title-1</a>
    <a href="#" tooltip title="Title-2" template="blue-tooltip">Title-2</a>
</div>

I could not apply different overriden tooltip css classes. 我无法应用其他覆盖的工具提示CSS类。

DEMO is here. 演示在这里。

Just change your link function to 只需将链接功能更改为

    link: function(scope, element, attrs){
        $(element).tooltip({
            title: scope.title,
            placement: "right",
        }).addClass(attrs.template);
    }

When it comes to Tooltips, it's not just about a single element, you need to style two different elements - arrow element and box element. 当涉及到工具提示时,它不仅是单个元素,还需要设置两个不同元素的样式- arrow元素和box元素。

As you can see here , the rendered markup for Tooltip contains two divs- tooltip-arrow & tooltip-inner , so you need to style both of them for the complete tooltip to be of same color. 如您在此处看到的,Tooltip的渲染标记包含两个divs tooltip-arrowtooltip-inner ,因此您需要对它们两者进行样式设置,以使整个工具提示具有相同的颜色。

Before that, you need to identify the information about type of Tooltip (red or blue) you want after the rendering. 在此之前,您需要标识有关渲染后所需的工具提示类型(红色或蓝色)的信息。 You can do this in 2 ways 您可以通过2种方式完成此操作

  1. Add a CSS class to the element as shown in Ivan's answer like below 如下所示,将CSS类添加到元素中,如Ivan的答案所示

     link: function(scope, element, attrs){ $(element).tooltip({ title: scope.title, placement: "right", }).addClass(attrs.template); } 
  2. Use the template attribute from your original markup and then modify your CSS selectors accordingly. 使用原始标记中的template属性,然后相应地修改CSS选择器。

I prefer class-selectors instead of attribute-selectors, so I defined the styles for your red Tooltip like below. 我更喜欢使用类选择器而不是属性选择器,因此我为红色工具提示定义了样式,如下所示。

.red-tooltip + .tooltip.right .tooltip-arrow{ 
    border-right-color: red; 
}

.red-tooltip + .tooltip .tooltip-inner{ 
    background-color: red ; 
    color: white;
}

We're using adjacent selector here to find the sibling of .red-tooltip class with a .tooltip class which has both .tooltip-arrow & .tooltip-inner divs. 我们在这里使用相邻的选择器来查找具有.tooltip类和.tooltip-arrow.tooltip-inner div的.tooltip .red-tooltip类的同级对象。 With that, both the arrow and inner divs will have same color and you will have your red-tooltip. 这样,箭头和内部div将具有相同的颜色,并且您将获得红色的工具提示。

Here's a sample Fiddle with the above mentioned changes. 这是带有上述更改的样本小提琴

Note : The hard part with Tooltips is styling Arrow elements. 注意:工具提示的难点是设置箭头元素的样式。 For each specific position like top, right, bottom, left - we need to write separate styles for border-colors. 对于每个特定的位置,例如顶部,右侧,底部,左侧-我们需要为边框颜色编写单独的样式。 In your case as you're using right as placement for Tooltip, I have border-right-color for tooltip-arrow class. 在您的情况下,因为您在Tooltip tooltip-arrow类中使用right作为Tooltip的放置位置,所以我使用了border-right-color In case you want to support all the other placements, you need to write styles for all those placements like below. 如果您想支持所有其他展示位置,则需要为所有这些展示位置编写样式,如下所示。

.red-tooltip + .tooltip.left .tooltip-arrow{ 
    border-left-color: red; 
}

.red-tooltip + .tooltip.bottom .tooltip-arrow{ 
    border-bottom-color: red; 
}

Hope this helps :) 希望这可以帮助 :)

Just change your css. 只需更改您的CSS。 goto your jsfiddle link and copy & paste bellow class 转到您的jsfiddle链接并复制并粘贴波纹管类

.tooltip-inner {background-color: #f00;} .tooltip-inner {background-color:#f00;}

your problems is solved i think. 我认为您的问题已经解决。

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

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