用HTML代码给自己的网站增加实时新闻热点

用HTML代码给自己的网站增加实时新闻热点


将以下代码复制到自己的网站即可实现


<!-- 今日头条热点新闻组件 -->
<div class="toutiao-container">
  <div class="toutiao-header">
    <div class="toutiao-title">
      <i class="fas fa-fire"></i>
      <span>今日头条热点榜</span>
    </div>
    <button class="refresh-btn" id="refreshBtn">
      <i class="fas fa-sync-alt"></i>
      <span>刷新</span>
    </button>
  </div>
  
  <ul class="news-list" id="newsList">
    <div class="loading">
      <i class="fas fa-spinner fa-spin"></i>
      <span>正在加载热点新闻...</span>
    </div>
  </ul>
</div>

<!-- 引入Font Awesome -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">

<style>
.toutiao-container {
  background: transparent;
  border-radius: 8px;
  overflow: hidden;
  margin: 15px 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "PingFang SC", "Microsoft YaHei", sans-serif;
}

.toutiao-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  border-bottom: 1px solid rgba(0,0,0,0.08);
  margin-bottom: 8px;
}

.toutiao-title {
  font-size: 18px;
  font-weight: 600;
  color: #e44;
  display: flex;
  align-items: center;
  gap: 8px;
}

.refresh-btn {
  background: transparent;
  border: none;
  color: #666;
  font-size: 14px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 5px;
  transition: all 0.3s ease;
}

.refresh-btn:hover {
  color: #e44;
}

.news-list {
  list-style: none;
  background: transparent;
}

.news-item {
  padding: 12px 16px;
  border-bottom: 1px solid rgba(0,0,0,0.05);
  transition: all 0.2s ease;
}

.news-item:hover {
  background: rgba(0,0,0,0.02);
}

.news-link {
  text-decoration: none;
  color: #333;
  display: flex;
  align-items: flex-start;
  gap: 12px;
}

.news-rank {
  min-width: 24px;
  height: 24px;
  background: #f0f0f0;
  border-radius: 4px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 14px;
  font-weight: bold;
  color: #666;
  margin-top: 2px;
  flex-shrink: 0;
}

.top-1 { background: linear-gradient(to right, #ff8a00, #ff4800); color: white; }
.top-2 { background: linear-gradient(to right, #ff9a00, #ff6a00); color: white; }
.top-3 { background: linear-gradient(to right, #ffaa00, #ff8a00); color: white; }

.news-content {
  flex-grow: 1;
}

.news-title {
  font-size: 16px;
  margin-bottom: 4px;
  font-weight: 500;
  transition: all 0.2s;
}

.news-link:hover .news-title {
  color: #e44;
}

.news-hotness {
  font-size: 12px;
  color: #f55;
  display: flex;
  align-items: center;
  gap: 4px;
}

.loading {
  padding: 30px;
  text-align: center;
  color: #666;
  font-size: 14px;
}

.error {
  padding: 20px;
  text-align: center;
  color: #e44;
  font-size: 14px;
}
</style>

<script>
document.addEventListener('DOMContentLoaded', function() {
  const newsList = document.getElementById('newsList');
  const refreshBtn = document.getElementById('refreshBtn');
  
  async function fetchHotNews() {
    try {
      newsList.innerHTML = `
        <div class="loading">
          <i class="fas fa-spinner fa-spin"></i>
          <span>正在加载热点新闻...</span>
        </div>
      `;
      
      const response = await fetch('https://api.pearktrue.cn/api/dailyhot/?title=今日头条');
      const data = await response.json();
      
      renderNewsList(data.data);
    } catch (error) {
      console.error('获取新闻数据失败:', error);
      newsList.innerHTML = `
        <div class="error">
          <i class="fas fa-exclamation-triangle"></i>
          <span>加载新闻失败,请稍后再试</span>
        </div>
      `;
    }
  }
  
  function renderNewsList(newsItems) {
    if (!newsItems || newsItems.length === 0) {
      newsList.innerHTML = `
        <div class="error">
          <i class="fas fa-exclamation-triangle"></i>
          <span>没有获取到新闻数据</span>
        </div>
      `;
      return;
    }
    
    const limitedNews = newsItems.slice(0, 15);
    let newsHTML = '';
    
    limitedNews.forEach((item, index) => {
      const rankClass = index < 3 ? `top-${index+1}` : '';
      
      newsHTML += `
        <li class="news-item">
          <a href="${item.url}" class="news-link" target="_blank" rel="noopener">
            <div class="news-rank ${rankClass}">${index + 1}</div>
            <div class="news-content">
              <div class="news-title">${item.title}</div>
              <div class="news-hotness">
                <i class="fas fa-fire"></i>
                <span>${item.hot}</span>
              </div>
            </div>
          </a>
        </li>
      `;
    });
    
    newsList.innerHTML = newsHTML;
  }
  
  fetchHotNews();
  refreshBtn.addEventListener('click', fetchHotNews);
});
</script>

264 ° 来自:PC IANA保留地址
上一篇: 一段代码给自己的网站增加一个24小时在线的AI客服对话功能
下一篇: 宋佳乐博客万能工具箱:你的一站式在线实用工具平台
您可能还喜欢这些:
版权声明 1 本网站名称:宋佳乐博客
2 本站永久网址:www.songjiale.com
3 本网站的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,请联系站长 QQ2801910651进行删除处理。
4 本站一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
5 本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报
6 本站资源大都存储在云盘,如发现链接失效,请联系我们我们会第一时间更新。
7 如无特别声明本文即为原创文章仅代表个人观点,版权归《宋佳乐博客》所有,欢迎转载,转载请保留原文链接。
8 如果您想第一时间收到本博最新文章可微信搜索公众号《佳乐博客》关注公众号第一时间获取最新消息。
广告位联系QQ2801910651 广告位联系QQ2801910651丨或者在线留言留下您的联系方式我们客服主动与您联系! Document
提醒图片

您已进入网站内部页面,请勿相信各种广告链接,请不要在本站输入任何账号密码!!!

亲,沙发正空着,还不快来抢?

评论审核已开启 记住我的个人信息 回复后邮件通知我
Back to Top