2024-08-18

以下是实现烟花绽放效果的核心CSS代码,包括元素的创建、样式定义以及JavaScript中的动画控制。




<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
  body {
    overflow: hidden;
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #000;
  }
 
  .fireworks {
    position: absolute;
    --c: #fff;
    --size: 2px;
    --speed: 500ms;
    --duration: 10s;
    --delay: 0s;
    --easing: linear;
  }
 
  .fireworks-item {
    position: absolute;
    width: var(--size);
    height: var(--size);
    background: var(--c);
    animation: fly var(--speed) var(--easing) infinite;
    opacity: 0;
    r: 100;
  }
 
  @keyframes fly {
    0% {
      opacity: 0;
      r: 100;
    }
    100% {
      opacity: 1;
      r: 10;
    }
  }
</style>
</head>
<body>
<script>
  const fireworks = (c, n, size, speed, duration, delay) => {
    const fireworks = document.createElement('div');
    fireworks.className = 'fireworks';
    Object.assign(fireworks.style, {
      '--c': c,
      '--size': `${size}px`,
      '--speed': `${speed}ms`,
      '--duration': `${duration}ms`,
      '--delay': `${delay}ms`,
    });
    for (let i = 0; i < n; i++) {
      const fireworksItem = document.createElement('div');
      fireworksItem.className = 'fireworks-item';
      fireworks.appendChild(fireworksItem);
    }
    document.body.appendChild(fireworks);
  };
 
  fireworks('#FFF', 100, 2, 500, 10000, 0);
</script>
</body>
</html>

这段代码创建了一个名为 .fireworks 的元素,并给它添加了一百个 .fireworks-item 作为烟花的粒子。每个烟花粒子都是一个 div,通过CSS动画从全透明到完全可见,大小从2px变化到10px,并且在屏幕上随机移动。通过调整CSS变量,可以快速改变烟花的颜色、大小和数量等特性。

2024-08-18

以下是一个简单的新浪新闻页面的HTML和CSS实现示例。由于完整代码较长,以下仅展示关键部分。

HTML:




<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>新浪新闻</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="header">
        <img src="logo.png" alt="新浪新闻logo">
        <h1>新闻头条</h1>
        <input type="text" placeholder="搜索新闻">
    </div>
 
    <div class="nav">
        <ul>
            <li><a href="#">首页</a></li>
            <li><a href="#">国际</a></li>
            <li><a href="#">国内</a></li>
            <li><a href="#">体育</a></li>
            <li><a href="#">娱乐</a></li>
        </ul>
    </div>
 
    <div class="content">
        <!-- 新闻内容 -->
    </div>
 
    <div class="footer">
        &copy; 2023 新浪新闻版权所有
    </div>
</body>
</html>

CSS (style.css):




body {
    font-family: Arial, sans-serif;
    margin: 0;
    padding: 0;
}
 
.header {
    background-color: #444;
    overflow: hidden;
    padding: 20px 0;
}
 
.header img {
    float: left;
    margin-right: 20px;
}
 
.header h1 {
    color: white;
    margin-left: 20px;
    font-size: 24px;
}
 
.header input {
    float: right;
    padding: 6px;
    margin-top: 10px;
}
 
.nav ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
}
 
.nav li {
    display: inline;
    margin-right: 20px;
}
 
.nav a {
    color: #fff;
    text-decoration: none;
}
 
.content {
    padding: 15px;
}
 
.footer {
    background-color: #444;
    color: white;
    text-align: center;
    padding: 20px 0;
}

这个示例展示了一个简单的新闻头条网站的头部(header)、导航栏(nav)、内容区域(content)和页脚(footer)的HTML和CSS结构。实际的新闻内容需要通过后端代码填充。

2024-08-18



import React from 'react';
import { Link } from 'react-router-dom';
import siteLogo from '../images/logo.svg';
 
const Navbar = () => {
  return (
    <nav className="bg-gray-800">
      <div className="max-w-7xl mx-auto px-2 sm:px-6 lg:px-8">
        <div className="relative flex items-center justify-between h-16">
          <div className="flex-1 flex items-center justify-center sm:items-stretch sm:justify-start">
            <div className="flex-shrink-0 flex items-center">
              <img
                className="block lg:hidden h-8 w-auto"
                src={siteLogo}
                alt="Workflow"
              />
              <img
                className="hidden lg:block h-8 w-auto"
                src={siteLogo}
                alt="Workflow"
              />
            </div>
            <div className="hidden sm:block sm:ml-6">
              <div className="flex space-x-4">
                <Link
                  to="/"
                  className="bg-gray-900 text-white px-3 py-2 rounded-md text-sm font-medium"
                >
                  Dashboard
                </Link>
                <Link
                  to="/settings"
                  className="text-gray-300 hover:bg-gray-700 hover:text-white px-3 py-2 rounded-md text-sm font-medium"
                >
                  Settings
                </Link>
              </div>
            </div>
          </div>
          <div className="absolute inset-y-0 right-0 flex items-center pr-2 sm:static sm:inset-auto sm:ml-6 sm:pr-0">
            <button className="bg-gray-800 p-1 rounded-full text-gray-400 hover:text-white focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-offset-gray-800 focus:ring-white">
              <span className="sr-only">View notifications</span>
              {/* Tailwind CSS icon */}
              <svg
                className="h-6 w-6"
                xmlns="http://www.w3.org/2000/svg"
                fill="none"
                viewBox="0 0 24 24"
                stroke="currentColor"
                aria-hidden="true"
              >
                <path
                  strokeLinecap="round"
                  strokeLinejoin="round"
                  strokeWidth="2"
                  d="M15 17h5l-1.40.59.01-2.01A5.9
2024-08-18

Normalize.css 是一种 CSS 文件,旨在减少不同浏览器之间的差异。它保留了有用的默认值,同时修复了常见的不一致性和不合标准的问题。

以下是一个简化的 Normalize.css 示例,它主要针对是 HTML5 和使用 UTF-8 编码的文档。




/* 移除默认边距 */
body, h1, h2, h3, h4, h5, h6, p, figure, blockquote, dl, dd {
  margin: 0;
}
 
/* 移除段落间距 */
p {
  margin: 1em 0;
}
 
/* 设置默认字体和背景 */
html {
  font-family: sans-serif;
  background: white;
  color: black;
}
 
/* 设置列表样式为无 */
ol, ul, li {
  list-style: none;
}
 
/* 设置超链接为蓝色并在Hover时改变样式 */
a {
  color: blue;
  text-decoration: none;
}
 
a:hover {
  text-decoration: underline;
}
 
/* 设置图片可点击并在点击时改变样式 */
img {
  max-width: 100%;
  height: auto;
  border: none;
}
 
img:focus {
  outline: none;
}
 
/* 设置表格边框合并 */
table {
  border-collapse: collapse;
}
 
/* 设置文本框和按钮的一致性 */
input, button, textarea {
  font: inherit;
}
 
/* 设置所有元素可见性 */
[hidden] {
  display: none;
}

这个示例提供了一个基础的 Normalize.css 样式表,它包括了移除默认边距和样式、设置字体和背景、移除列表样式、设置链接样式、优化图片和表格显示等。这样的样式表可以确保跨浏览器的一致性,同时为开发者提供了一个基本的起点来构建他们的网页样式。

2024-08-18



<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>初探CSS与HTML的融合</title>
    <style>
        body {
            font-family: 'Microsoft YaHei', sans-serif;
            background-color: #F7F7F7;
        }
        .container {
            max-width: 600px;
            margin: 0 auto;
            padding: 20px;
            background-color: #FFF;
            border: 1px solid #EEE;
            box-sizing: border-box;
        }
        h1 {
            color: #333;
            text-align: center;
        }
        p {
            color: #666;
            font-size: 18px;
            line-height: 1.5;
        }
        a {
            color: #1088e4;
            text-decoration: none;
        }
        a:hover {
            text-decoration: underline;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>欢迎来到我的网站</h1>
        <p>这里是网站的介绍文字,可以通过CSS来控制样式。</p>
        <p>
            <a href="https://www.example.com">点击这里访问我的网站</a>
        </p>
    </div>
</body>
</html>

这段代码展示了如何在HTML文件中嵌入CSS样式,并通过class选择器为页面元素添加样式。同时也演示了如何使用<style>标签内嵌样式,以及如何通过链接外部CSS文件来控制样式。这是学习Web开发的基础,有助于理解前端开发的核心概念。

2024-08-18

在HTML和CSS方面,有许多的技巧和最佳实践可以提高代码的效率和可维护性。以下是一些常见的提高HTML和CSS编写质量的方法:

  1. 良好的HTML结构:

    • 使用语义化的HTML标签,如<header>, <nav>, <section>, <article>, <footer>等。
    • 正确嵌套HTML元素,确保每个元素都有清晰的父子关系。
    • 使用HTML注释来描述代码块的功能。
  2. 优化CSS选择器:

    • 避免使用标签名作为选择器,应该使用类或者ID。
    • 使用高效的CSS选择器,如.parent > .child代替.parent .child
    • 使用CSS类来控制样式,避免重复的样式规则。
  3. 代码可维护性:

    • 使用BEM(Block Element Modifier)或者其他命名约定来增强CSS可维护性。
    • 使用CSS预处理器(如Sass或Less)来提取共同的样式和变量。
    • 使用CSS工具如Autoprefixer自动添加浏览器前缀。
  4. 性能优化:

    • 优化CSS文件的大小,可以通过压缩和合并来实现。
    • 使用懒加载或预加载来优化资源加载时机。
    • 使用Web性能工具来分析并优化页面加载速度。
  5. 访问性优化:

    • 使用alt属性给图像添加描述。
    • 使用title属性来增加额外的信息。
    • 使用label标签为表单元素提供标签。
  6. 代码质量检查和审查:

    • 使用工具如W3C Markup Validation Service和CSS Validation Service检查HTML和CSS代码的有效性。
    • 通过同事审查或使用HTML和CSS的验证工具来找出潜在的问题。
  7. 响应式设计:

    • 使用媒体查询来实现响应式设计。
    • 使用相对单位(如%,em,rem)而不是固定单位(如px)。
    • 使用Flexbox或Grid布局系统来实现灵活的布局。
  8. 代码组织和注释:

    • 使用有意义的ID和类名来组织代码。
    • 添加有意义的注释来描述复杂的逻辑或代码段落。

以下是一个简单的HTML和CSS示例,展示了如何使用一些这些最佳实践:




<!-- 良好的HTML结构 -->
<header>
  <nav>
    <ul>
      <li><a href="/">Home</a></li>
      <li><a href="/about">About</a></li>
    </ul>
  </nav>
</header>
<section class="main-content">
  <h1 class="title">This is a page title</h1>
  <p>This is a paragraph of text...</p>
  <!-- 其他内容 -->
</section>
<footer>
  <p>Copyright © 2023 Your Company</p>
</footer>



/* 优化CSS选择器 */
.main-content > p {
  color: #333;
  /* 其他样式 */
}
 
/* 使用BEM命名方法 */
.block {
  display: flex;
}
.block__element {
  padding: 10px;
}
.block__element--modifier {
  background-color: #f00;
}
 
/* 预处理器中的变量和导
2024-08-18

要使用CSS画一个半圆弧线并加上圆圈定位,可以使用以下方法:

HTML:




<div class="half-circle">
  <div class="circle"></div>
</div>

CSS:




.half-circle {
  position: relative;
  width: 100px;
  height: 50px;
  overflow: hidden;
}
 
.half-circle:before {
  content: '';
  position: absolute;
  top: 0;
  left: 50%;
  width: 100%;
  height: 100%;
  background-color: #ff0000;
  border-radius: 50%;
  transform: translateX(-50%);
}
 
.circle {
  position: absolute;
  top: 0;
  left: 50%;
  width: 50px;
  height: 50px;
  background-color: #ffffff;
  border-radius: 50%;
  transform: translateX(-50%);
}

这段代码创建了一个宽100px, 高50px的容器.half-circle,使用:before伪元素创建了一个半圆弧,并通过overflow: hidden隐藏了右半部分。容器内的.circle用来作为白色圆圈,通过定位和border-radius属性实现圆形。

2024-08-18

以下是一个简单的八卦图案的HTML和CSS代码示例:




<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Bagua Pattern</title>
<style>
  .bagua {
    position: relative;
    width: 200px;
    height: 200px;
    border: 4px solid black;
    border-radius: 50%;
    margin: 50px auto;
  }
  .bagua:before, .bagua:after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
  }
  .bagua:before {
    width: 100px;
    height: 150px;
    background-color: black;
    border-radius: 0 0 50% 50%;
  }
  .bagua:after {
    width: 150px;
    height: 100px;
    background-color: black;
    border-radius: 50% 0 0 0;
    transform: translate(-50%, -50%) rotate(45deg);
  }
</style>
</head>
<body>
<div class="bagua"></div>
</body>
</html>

这段代码创建了一个类.bagua,它用CSS伪元素:before:after形成了八卦图案。.bagua类定义了一个圆形的容器,并设置了边框和尺寸。伪元素用不同的尺寸、背景色和边径创建了八卦图案的两个部分。

2024-08-18

以下是一个简化版的postcss-px-to-viewport插件的核心函数示例,用于实现样式中的像素单位转换为视口单位。




const postcss = require('postcss');
 
module.exports = postcss.plugin('add-px-to-viewport', opts => {
  return function(root) {
    root.walkDecls(decl => {
      decl.value = decl.value.replace(/(\d+(\.\d+)?)(px)/gi, (match, number) => {
        return `${parseFloat(number) / opts.viewportWidth * 100}vw`;
      });
    });
  };
});

使用方法:

  1. 安装postcss(如果尚未安装):



npm install postcss --save-dev
  1. postcss.config.js中配置插件:



module.exports = {
  plugins: [
    require('./path/to/your/plugin')({ viewportWidth: 750 }) // 假设设计稿宽度为750px
  ]
};
  1. 在CSS文件中写入像素单位:



/* input.css */
.some-class {
  width: 300px;
  height: 200px;
}
  1. 使用PostCSS处理文件:



const postcss = require('postcss');
const fs = require('fs');
 
postcss([require('./path/to/your/plugin')({ viewportWidth: 750 })])
  .process(fs.readFileSync('input.css', 'utf8'), { from: 'input.css', to: 'output.css' })
  .then(result => {
    fs.writeFileSync('output.css', result.css);
  });

输出将是转换后的视口单位CSS文件:




/* output.css */
.some-class {
  width: 426.666666666666666667vw;
  height: 156.25vw;
}
2024-08-18

以下是一个简化的示例,展示了如何使用CSS创建一个简单的动态雷达扫描效果:




<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dynamic Radar Scan</title>
<style>
  .radar {
    position: relative;
    width: 200px;
    height: 200px;
    border: 16px solid #3498db;
    border-radius: 50%;
    margin: 50px;
  }
  .radar::before {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 20px;
    height: 20px;
    background-color: #3498db;
    border-radius: 50%;
    animation: rotate 2s linear infinite;
  }
  @keyframes rotate {
    from {
      transform: rotate(0deg);
    }
    to {
      transform: rotate(360deg);
    }
  }
</style>
</head>
<body>
<div class="radar"></div>
</body>
</html>

这段代码创建了一个圆形的雷达扫描效果,通过CSS动画rotate实现不断旋转。通过调整.radar的尺寸和.radar::before的大小和颜色,可以进一步定制这个效果。