html+css:按钮上显示消息数量精简代码

效果如图
这里写图片描述
这个很多人可能没思路,其实就是a标签里面加个显示数量的div

<a href="index.html" class="nav-link">aaa<div class="nav-counter nav-counter-blue">4</div></a>

但是复杂的是css代码,如下

.nav-link {
  position: relative;
  padding: 0 14px;
  line-height: 34px;
  font-size: 10px;
  font-weight: bold;
  color: #555;
  text-decoration: none;
}
.nav-link:hover {
  color: #333;
  text-decoration: underline;
}

.nav-counter {
  position: absolute;
  top: -1px;
  right: 1px;
  min-width: 8px;
  height: 20px;
  line-height: 20px;
  margin-top: -11px;
  padding: 0 6px;
  font-weight: normal;
  color: white;
  text-align: center;
  text-shadow: 0 1px rgba(0, 0, 0, 0.2);
  background: #e23442;
  border: 1px solid #911f28;
  border-radius: 11px;
  background-image: -webkit-linear-gradient(top, #e8616c, #dd202f);
  background-image: -moz-linear-gradient(top, #e8616c, #dd202f);
  background-image: -o-linear-gradient(top, #e8616c, #dd202f);
  background-image: linear-gradient(to bottom, #e8616c, #dd202f);
  -webkit-box-shadow: inset 0 0 1px 1px rgba(255, 255, 255, 0.1), 0 1px rgba(0, 0, 0, 0.12);
  box-shadow: inset 0 0 1px 1px rgba(255, 255, 255, 0.1), 0 1px rgba(0, 0, 0, 0.12);
}

.nav-counter-green {
  background: #75a940;
  border: 1px solid #42582b;
  background-image: -webkit-linear-gradient(top, #8ec15b, #689739);
  background-image: -moz-linear-gradient(top, #8ec15b, #689739);
  background-image: -o-linear-gradient(top, #8ec15b, #689739);
  background-image: linear-gradient(to bottom, #8ec15b, #689739);
}

.nav-counter-blue {
  background: #3b8de2;
  border: 1px solid #215a96;
  background-image: -webkit-linear-gradient(top, #67a7e9, #2580df);
  background-image: -moz-linear-gradient(top, #67a7e9, #2580df);
  background-image: -o-linear-gradient(top, #67a7e9, #2580df);
  background-image: linear-gradient(to bottom, #67a7e9, #2580df);
}

html十分精简,只有一句话,但是css比较复杂,主要是a标签的position: relative,里面显示消息数量的div设成position:absolute,然后就有了重叠效果。

  • 9
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
以下是一个简单的示例代码,实现了通过按钮切换不同新闻公告内容: HTML代码: ```html <div class="news"> <div class="news-header"> <button class="active" onclick="showNews('news1')">新闻1</button> <button onclick="showNews('news2')">新闻2</button> <button onclick="showNews('news3')">新闻3</button> </div> <div class="news-content"> <div id="news1" class="news-item active"> <h2>这是新闻1的标题</h2> <p>这是新闻1的内容</p> </div> <div id="news2" class="news-item"> <h2>这是新闻2的标题</h2> <p>这是新闻2的内容</p> </div> <div id="news3" class="news-item"> <h2>这是新闻3的标题</h2> <p>这是新闻3的内容</p> </div> </div> </div> ``` CSS代码: ```css .news-header button { background-color: #eee; border: none; padding: 10px 20px; margin-right: 10px; cursor: pointer; } .news-header button.active { background-color: #ccc; } .news-content .news-item { display: none; } .news-content .news-item.active { display: block; } ``` JS代码: ```javascript function showNews(newsId) { // 隐藏所有新闻内容 var newsItems = document.querySelectorAll('.news-item'); for (var i = 0; i < newsItems.length; i++) { newsItems[i].classList.remove('active'); } // 显示指定的新闻内容 var newsItem = document.getElementById(newsId); newsItem.classList.add('active'); // 切换按钮的状态 var buttons = document.querySelectorAll('.news-header button'); for (var i = 0; i < buttons.length; i++) { buttons[i].classList.remove('active'); } var button = document.querySelector('.news-header button[data-news="' + newsId + '"]'); button.classList.add('active'); } ``` 这段代码中,我们通过 HTMLCSS 定义了一个新闻公告的基本结构和样式,然后通过 JS 实现了按钮的切换功能。当用户点击某个按钮时,我们会先隐藏所有的新闻内容,然后显示对应的新闻内容,并切换按钮的状态(将当前按钮设为激活状态,其它按钮设为非激活状态)。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我扶奶奶过哈登

您的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值