简体   繁体   English

动态变化:伪背景色后

[英]Dynamically change :after pseudo background-color

I am creating buttons within a repeater like this (simplyfied):我正在这样的中继器中创建按钮(简化):

    <asp:Repeater runat="server" OnItemDataBound="Repeater_Buttons_ItemDataBound">
        <ItemTemplate>
            <telerik:RadButton runat="server" CssClass="myButton" ID="MyButton">
                <ContentTemplate>
                   <...>
                </ContentTemplate>
            </telerik:RadButton>
        </ItemTemplate>
    </asp:Repeater>

those buttons are styled with:after pseudo to create a colored background swiping in the background of this buttons这些按钮的样式为:after pseudo 以创建彩色背景在此按钮的背景中滑动

.myButton {
    ...
}

   .myButton::after {
        content: "";
        display: block;
        width: 45px;
        height: 45px;
        background-color: var(--myColor);
        position: absolute;
        left: 0;
        top: 0;
        transition: 400ms all;
        z-index: 1;
    }

the result looks like this:结果如下所示:

在此处输入图像描述

my problem is, I need to change the color of this:after content dynamically based on the item bounded.我的问题是,我需要更改 this:after content 的颜色,动态基于项目边界。

I can access the RadButton on ItemDataBound where I tried to change it via the Attributes without success.我可以访问 ItemDataBound 上的 RadButton,我试图通过属性更改它但没有成功。

protected void Repeater_Buttons_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        var currentItem = (e.Item.DataItem as MyItem);

        var radButton = (e.Item.FindControl("MyButton") as RadButton);

        // whishful function I need:
        radButton.After.BackgroundColor = currentItem.BackColor;

        // or something like 
        radButton.Attributes.Add("style:after", $"background-color: {currentItem.BackColor}");

    }
}

Also approaches via javascript where unsuccessful.也可以通过 javascript 尝试,但不成功。 I cannot create diffrente css classes hardcode since any color can be choosen for the different items我无法创建不同的 css 类硬编码,因为可以为不同的项目选择任何颜色

IMHO, you don't have to override existing CSS.恕我直言,您不必覆盖现有的 CSS。 Your CSS already has你的 CSS 已经有了

.myButton::after {
    ...
    background-color: var(--myColor);
    ...
}

So you need just to change this --myColor CSS variable.所以你只需要改变这个--myColor CSS 变量。 Even without a server call.即使没有服务器调用。 All on a client.都在一个客户端上。

For a fast example, plain JS can do it.举个简单的例子,普通的 JS 就可以做到。 Such JS action could be built-in into a click handler.这样的 JS 操作可以内置到点击处理程序中。

const root = document.documentElement; //get the root element
...
root.style.setProperty('--myColor', someColorForThisMoment);

Or such code could be a part of an EventListener (randomly change --myColor on each click):或者这样的代码可以是 EventListener 的一部分(每次点击时随机更改--myColor ):

function random(min,max) {
  const num = Math.floor(Math.random()*(max-min)) + min;
  return num;
}

function getRandomColor() {
  return 'rgb(' + random(0,255) + ', ' + random(0,255) + ', ' + random(0,255) +  ')';
}

const root = document.documentElement;
root.addEventListener('click', e => {
  const newRandomColor = getRandomColor();
  root.style.setProperty('--myColor', newRandomColor);
});

UI styling is a client job. UI 样式是客户的工作。 Don't overload your server with redundant calls.不要用冗余调用使服务器过载。

Maybe you can just add an extra/different class to the Button instead of trying to add inline styles?也许您可以向 Button 添加一个额外的/不同的类,而不是尝试添加内联样式? See the example below (without the telerik controls)请参阅下面的示例(没有 Telerik 控件)

<asp:Repeater ID="Repeater_Buttons" runat="server" OnItemDataBound="Repeater_Buttons_ItemDataBound">
    <ItemTemplate>

        <div>
            <asp:LinkButton ID="MyButton" runat="server" CssClass="myButton">LinkButton</asp:LinkButton>
        </div>

    </ItemTemplate>
</asp:Repeater>


<style>
    .myButton,
    .myButtonDynamic {
        background-color: red;
    }

        .myButton::after {
            content: "after";
            background-color: yellow;
        }

        .myButtonDynamic::after {
            content: "after";
            background-color: green;
        }
</style>

And then in the ItemDataBound然后在 ItemDataBound

protected void Repeater_Buttons_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.AlternatingItem)
    {
        var radButton = (LinkButton)e.Item.FindControl("MyButton");

        radButton.CssClass = "myButtonDynamic";
    }
}

Assuming the currentItem.BackColor is already formatted as a CSS colour string, this should work:假设currentItem.BackColor已经格式化为 CSS 颜色字符串,这应该可以工作:

var radButton = (e.Item.FindControl("MyButton") as RadButton);
radButton.Style["--myColor"] = currentItem.BackColor;

If it's a System.Drawing.Color , then the ColorTranslator.ToHtml method should give you the required string.如果它是System.Drawing.Color ,则ColorTranslator.ToHtml方法应该为您提供所需的字符串。

The simplest solution is to use the inheritance feature of CSS.最简单的解决方案是使用 CSS 的继承特性。 Suppose your CSS is setup like this (only relevant properties shown):假设您的 CSS 是这样设置的(仅显示相关属性):

:root {
  --myColor: red;
}
.myButton {
}
.myButton::after {
  background-color: var(--myColor);
}

Now, inheritance allows you to do this:现在,继承允许你这样做:

<!-- the inline style will trump the rule defined inside stylesheet -->
<div class="myButton" style="--myColor: green;">This should be green</div>

 :root { --myColor: red; } .myButton { } .myButton::after { content: " ::after"; background-color: var(--myColor); }
 <div class="myButton">default color</div> <div class="myButton">default color</div> <div class="myButton" style="--myColor: green;">custom color</div> <div class="myButton" style="--myColor: blue;">custom color</div>

So basically all you need to do inside your server side code is this:所以基本上你需要在服务器端代码中做的就是:

var currentItem = (e.Item.DataItem as MyItem);
var radButton = (e.Item.FindControl("MyButton") as RadButton);
// not familiar with C# but I am sure it is possible to
// manipulate the style attribute of a server side control
radButton.Styles["--myColor"] = currentItem.BackColor;

You could just write to the custom property您可以只写入自定义属性

protected void Repeater_Buttons_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        var currentItem = (e.Item.DataItem as MyItem);

        var radButton = (e.Item.FindControl("MyButton") as RadButton);

        radButton.Attributes.Add("style", $"--myColor: {currentItem.BackColor}");

    }
}

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

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