CSS元素角标设置

 实现效果:

 代码:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>元素角标设置</title>
    <style>
      .box {
        width: 100%;
        margin-top: 10px;
        display: flex;
        flex-direction: row;
        flex-wrap: wrap;
      }
      .item {
        width: calc(50% - 30px);
        font-size: 12px;
        background: #f2f2f2;
        text-align: center;
        padding: 8px 5px;
        margin: 5px 10px;
        border-radius: 4px;
        overflow: hidden;
        /* 内容超出不换行 */
        white-space: nowrap; 
        text-overflow: ellipsis;
      }
      .sel {
        color: #4796e3;
        background-color: rgba(57, 125, 234, 0.0745098039215686);
        position: relative;
      }
      /* 实现角标背景 */
      .sel::before {
        position: absolute;
        content: "";
        right: 0px;
        bottom: 0px;
        width: 20px;
        height: 20px;
        clip-path: polygon(20px 20px, 20px 0, 0 0);
        /* 旋转元素90度 */
        transform: rotate(90deg);
        border-top-right-radius: 4px;
        background-color: #4796e3;
      }
      /* 使用div边框实现对号效果 */
      .sel::after {
        position: absolute;
        content: "";
        right: 4px;
        bottom: 3px;
        width: 3px;
        height: 6px;
        border-color: #fff;
        border-style: solid;
        border-width: 0 1px 1px 0;
        /* 旋转元素45度 */
        transform: rotate(45deg);
      }
    </style>
    <script src="./jquery.min.js"></script>
  </head>
  <body>
    <div class="box">
      <div class="item">111111111111111111111111111111111111111111111111111</div>
      <div class="item">22222</div>
      <div class="item">33333</div>
      <div class="item">44444</div>
    </div>

    <script>
      var box = document.querySelector(".box");
      box.addEventListener("click", function boxClick(e) {
        if (e.target.className.indexOf("item") > -1) {
          if (e.target.className.indexOf("sel") > -1) {
            $(e.target).removeClass("sel");
          } else {
            $(e.target).addClass("sel");
          }
        }
      });
    </script>
  </body>
</html>

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值