2024-08-25

在Vue 3中,内置的<Transition>组件可以用来包装需要过渡效果的元素,它会在元素的出现和消失过程中自动应用CSS效果。

下面是一个简单的例子,展示如何使用<Transition>组件:




<template>
  <button @click="show = !show">
    Toggle
  </button>
  <Transition name="fade">
    <p v-if="show">Hello World!</p>
  </Transition>
</template>
 
<script setup>
import { ref } from 'vue'
 
const show = ref(true)
</script>
 
<style>
/* 定义进入和退出的过渡效果 */
.fade-enter-active, .fade-leave-active {
  transition: opacity 0.5s ease;
}
.fade-enter-from, .fade-leave-to {
  opacity: 0;
}
</style>

在这个例子中,一个段落(<p>)通过v-if指令与show变量绑定,show变量的值通过按钮点击事件进行切换。<Transition>组件包裹了这个段落,并通过name属性指定了过渡的效果类名前缀"fade"。CSS中定义了两个类:.fade-enter-active.fade-leave-active用于指定进入和退出过渡的状态,而.fade-enter-from.fade-leave-to定义了初始状态和结束状态的样式。当show变量为true时显示段落,为false时段落消失,并应用淡入淡出的过渡效果。

2024-08-25

要使用原生JavaScript实现抽屉动画,你可以创建一个函数,该函数使用setIntervalrequestAnimationFrame来逐渐改变元素的CSS属性,从而创建平滑的动画效果。

以下是一个简单的例子,演示了如何使用requestAnimationFrame来实现一个抽屉动画:

HTML:




<div id="drawer" style="width: 200px; height: 100px; background-color: blue; transition: transform 0.5s; transform: translateX(0);">
  <!-- 抽屉内容 -->
</div>
<button id="toggleButton">Toggle Drawer</button>

CSS:




#drawer {
  transform: translateX(-200px); /* 初始时抽屉在容器外 */
}

JavaScript:




const drawer = document.getElementById('drawer');
const toggleButton = document.getElementById('toggleButton');
let isOpen = false;
 
toggleButton.addEventListener('click', () => {
  isOpen = !isOpen;
  animateDrawer(isOpen);
});
 
function animateDrawer(isOpen) {
  const start = isOpen ? -200 : 0; // 抽屉的起始和结束位置
  const end = isOpen ? 0 : -200;
  const distance = end - start;
  let isAnimating = true;
 
  requestAnimationFrame(function animate(time) {
    if (!isAnimating) return;
 
    const progress = (time - lastTime) / duration; // 使用当前时间和起始时间计算进度
    const position = start + distance * easeInOutQuad(progress); // 应用缓动函数计算当前位置
    drawer.style.transform = `translateX(${position}px)`;
 
    if (progress < 1) {
      requestAnimationFrame(animate); // 如果动画未完成,继续调用requestAnimationFrame
    } else {
      isAnimating = false; // 动画完成
    }
  });
}
 
// 缓动函数,控制动画的加速和减速
function easeInOutQuad(t) {
  return t < 0.5 ? 2 * t * t : -1 + (4 - 2 * t) * t;
}

在这个例子中,我们定义了一个animateDrawer函数,它接受一个布尔值isOpen来确定抽屉是打开还是关闭。使用requestAnimationFrame来迭代变换抽屉的transform属性,从而创建平滑的动画效果。我们还定义了一个easeInOutQuad函数来实现缓动效果,这样抽屉的打开和关闭就会有一个更自然的感觉。

2024-08-25

以下是一个简单的实现整屏滚动fullpage功能的HTML、CSS和JavaScript代码示例:




<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Fullpage Scroll Example</title>
<style>
  * {
    margin: 0;
    padding: 0;
  }
  html, body {
    height: 100%;
  }
  .fullpage {
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
  }
  .section {
    flex: 1;
    display: flex;
    justify-content: center;
    align-items: center;
    text-align: center;
  }
</style>
</head>
<body>
<div class="fullpage">
  <div class="section" style="background-color: #f0f0f0;">Section 1</div>
  <div class="section" style="background-color: #e0e0e0;">Section 2</div>
  <div class="section" style="background-color: #d0d0d0;">Section 3</div>
  <div class="section" style="background-color: #c0c0c0;">Section 4</div>
</div>
 
<script>
  document.body.addEventListener('wheel', (event) => {
    const scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
    const scrollHeight = document.documentElement.scrollHeight;
    const innerHeight = window.innerHeight;
    const isScrollingDown = event.deltaY > 0;
    const isScrollTop = scrollTop === 0;
    const isScrollBottom = scrollTop + innerHeight >= scrollHeight;
 
    if ((isScrollingDown && isScrollTop) || (!isScrollingDown && isScrollBottom)) {
      event.preventDefault();
    }
  }, { passive: false });
</script>
</body>
</html>

这段代码使用了HTML定义了一个包含四个部分的全屏滚动容器,CSS用于设置布局和样式,JavaScript用于处理滚动事件,确保了在到达顶部或底部时阻止滚动事件,实现了全屏滚动的效果。

2024-08-25

HTML中的<ol><ul><li>标签默认情况下可能会有向左的偏移,这是由于浏览器默认的CSS样式造成的。要解决这个问题,你可以使用CSS来重置这些标签的默认样式。

以下是一个简单的CSS示例,用于重置<ol><ul><li>的默认样式:




ol, ul {
  list-style: none; /* 移除列表的标记,比如圆圈或方块 */
  padding: 0; /* 移除默认的内边距 */
  margin: 0; /* 移除默认的外边距 */
}
 
li {
  margin-left: 0; /* 移除<li>的默认左边距 */
  padding-left: 0; /* 移除<li>的默认左内边距 */
}

将上述CSS代码添加到你的样式表中,可以去除<ol>, <ul>, 和 <li>标签的默认偏移。如果你没有使用外部样式表,可以在HTML文档的<head>标签内部添加<style>标签,并包含上述CSS代码:




<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Reset List Style</title>
<style>
  ol, ul {
    list-style: none;
    padding: 0;
    margin: 0;
  }
  li {
    margin-left: 0;
    padding-left: 0;
  }
</style>
</head>
<body>
<ol>
  <li>Item 1</li>
  <li>Item 2</li>
</ol>
<ul>
  <li>Item 1</li>
  <li>Item 2</li>
</ul>
</body>
</html>

这段代码将移除有序列表和无序列表的默认样式,并确保<li>元素没有左边距和左内边距。

2024-08-25

在CSS中,可以使用以下几种方法画出几何图形:

  1. 使用border属性画矩形、三角形等。
  2. 使用transform画平行四边形、菱形等。
  3. 使用clip-path画圆形、多边形等。
  4. 使用linear-gradient和background-image画线、圆、椭圆、多边形等。

以下是一些示例代码:

  1. 使用border画矩形:



.rectangle {
  width: 100px;
  height: 50px;
  border: 1px solid black;
}
  1. 使用transform画平行四边形:



.parallelogram {
  width: 150px;
  height: 100px;
  transform: skew(-20deg);
  background-color: black;
}
  1. 使用clip-path画圆形:



.circle {
  width: 100px;
  height: 100px;
  background-color: black;
  clip-path: circle(50%);
}
  1. 使用linear-gradient和background-image画线:



.line {
  width: 200px;
  height: 50px;
  background-image: linear-gradient(to right, black 50%, transparent 50%);
}

这些方法可以画出各种简单的几何图形,但CSS本身不支持复杂的绘图操作,如果需要画更复杂的图形,通常需要借助其他技术,如SVG或Canvas。

2024-08-25

CSS实现动画效果,可以使用@keyframes规则定义动画,然后通过animation属性应用到元素上。以下是一个简单的示例,演示了如何使用CSS创建一个淡入效果:




/* 定义一个名为fadeIn的动画 */
@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}
 
/* 应用动画效果到具体元素 */
.element {
  /* 动画名称 | 持续时间 | 延迟 | 次数 | 方向 | 填充模式 | 是否暂停 | 时间函数 */
  animation: fadeIn 2s ease-in-out forwards;
}

HTML部分:




<div class="element">我是要淡入的内容</div>

这段代码会使得类名为element的元素在2秒内从完全透明渐渐变为完全不透明。ease-in-out是动画的时间函数,表示开始和结束慢,中间快;forwards是填充模式,表示动画结束后,元素将保留动画结束时的样式。

2024-08-25



<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>简洁美观的时间线</title>
<style>
    body {
        font-family: Arial, sans-serif;
        padding: 20px;
    }
    .timeline {
        position: relative;
        padding: 0;
        list-style: none;
    }
    .timeline-year {
        position: absolute;
        top: 0;
        left: 50%;
        transform: translateX(-50%);
        text-align: center;
        color: #999;
    }
    .timeline-year:before {
        content: '';
        position: absolute;
        top: 100%;
        left: 50%;
        transform: translateX(-50%);
        width: 16px;
        height: 16px;
        background: #444;
        border-radius: 50%;
    }
    .timeline-year:after {
        content: '';
        position: absolute;
        top: 100%;
        left: 50%;
        transform: translateX(-50%);
        width: 4px;
        height: 100px;
        background: #444;
    }
    .timeline-entry {
        position: relative;
        margin-left: 20px;
    }
    .timeline-entry:before {
        content: '';
        position: absolute;
        top: 0;
        left: 0;
        width: 10px;
        height: 10px;
        background: #444;
        border-radius: 50%;
    }
</style>
</head>
<body>
 
<ul class="timeline">
    <li class="timeline-year" style="left: 0%;">1900</li>
    <li class="timeline-entry">重要事件A</li>
    <li class="timeline-entry">重要事件B</li>
    <li class="timeline-year" style="left: 25%;">1950</li>
    <li class="timeline-entry">重要事件C</li>
    <li class="timeline-entry">重要事件D</li>
    <li class="timeline-year" style="left: 50%;">2000</li>
    <li class="timeline-entry">重要事件E</li>
    <li class="timeline-entry">重要事件F</li>
    <li class="timeline-year" style="left: 75%;">2050</li>
    <li class="timeline-entry">重要事件G</li>
    <li class="timeline-entry">重要事件H</li>
</ul>
 
</body>
</html>

这个简洁美观的时间线使用了HTML和CSS来创建。它使用了一个无序列表来表示时间线,并通过CSS样式化,包括年份的圆形标记、年表之间的连线和条目。每个时间条目被表示为列表项,并通过相对定位与绝对定位相结合来排列在正确的年份标记上。这个例子提供了一个简单的方法来创建一个可扩展和可维护的时间线布局。

2024-08-25

CSS3 在布局、背景、边框、文本、效果、2D/3D 转换以及动画等方面增加了许多新的属性。以下是一些常见的 CSS3 新增属性及其简单用法示例:

  1. 圆角(border-radius):



div {
  border: 2px solid #000;
  border-radius: 10px; /* 所有角都有圆角 */
}
  1. 阴影(box-shadow):



div {
  box-shadow: 5px 5px 10px #000; /* 水平偏移、垂直偏移、模糊距离、颜色 */
}
  1. 文字阴影(text-shadow):



p {
  text-shadow: 2px 2px 4px #000;
}
  1. 线性渐变(linear-gradient):



div {
  background: linear-gradient(to right, red, yellow);
}
  1. 旋转(transform: rotate):



div {
  transform: rotate(45deg); /* 顺时针旋转45度 */
}
  1. 缩放(transform: scale):



div:hover {
  transform: scale(1.5); /* 鼠标悬停时放大1.5倍 */
}
  1. 过渡效果(transition):



div {
  transition: all 0.5s ease-in-out; /* 所有属性变化在0.5秒内平滑过渡 */
}
div:hover {
  transform: rotate(45deg) scale(1.5);
}
  1. 用户界面(cursor):



div {
  cursor: pointer; /* 将鼠标光标变为手指形状 */
}
  1. 多列布局(column):



div {
  column-count: 3; /* 分为3列 */
  column-gap: 20px; /* 列与列之间的间隔 */
}
  1. 动画(animation):



@keyframes example {
  from { background-color: red; }
  to { background-color: yellow; }
}
div {
  animation-name: example;
  animation-duration: 4s;
  animation-iteration-count: infinite;
}

这些只是 CSS3 新增属性的一小部分,CSS3 还有许多其他的特性和改进,如 3D 转换、阴影DOM(Shadow DOM)、弹性盒(Flexbox)布局等。

2024-08-25

CSS可以通过媒体查询(Media Queries)实现响应式设计,自适应不同屏幕尺寸。使用百分比、vwvhflexgrid系统可以让div盒子宽度随着屏幕宽度变化而变化。

以下是一个简单的例子,使用CSS Flexbox来创建一个自适应布局的div容器:




.container {
  display: flex; /* 启用flex布局 */
  flex-wrap: wrap; /* 子元素超出父容器时自动换行 */
  gap: 10px; /* 子元素之间的间隔 */
}
 
.item {
  flex: 1; /* 子元素会均分父容器的宽度 */
  min-width: 150px; /* 最小宽度 */
  max-width: 250px; /* 最大宽度 */
  background-color: lightblue; /* 背景颜色 */
  padding: 10px; /* 内边距 */
}
 
/* 当屏幕宽度小于600px时,所有item占满屏幕宽度 */
@media (max-width: 600px) {
  .item {
    flex-basis: 100%; /* 子元素宽度设置为100% */
  }
}

HTML部分:




<div class="container">
  <div class="item">Item 1</div>
  <div class="item">Item 2</div>
  <div class="item">Item 3</div>
</div>

在上述例子中,.container 是一个flex容器,.item 是其子元素。在屏幕宽度大于600px时,.item 会有相同的宽度。当屏幕宽度小于600px时,通过媒体查询,.item 的宽度将自适应屏幕宽度,每个.item 占满一行。

2024-08-25



/* 设置滚动容器 */
.scroll-text-container {
  width: 100%;
  white-space: nowrap;
  overflow: hidden;
  box-sizing: border-box;
}
 
/* 设置滚动文本 */
.scroll-text {
  display: inline-block;
  padding-left: 100%;
  animation: scroll-left 10s linear infinite;
}
 
/* 定义关键帧动画 */
@keyframes scroll-left {
  from {
    transform: translateX(0);
  }
  to {
    transform: translateX(-100%);
  }
}
 
/* 示例用法 */
<div class="scroll-text-container">
  <div class="scroll-text">这是一个滚动的文本效果 - 欢迎访问我们的网站</div>
</div>

这段代码定义了一个简单的文字滚动效果。.scroll-text-container 是一个容器,它设置了滚动的宽度和滚动的基本属性。.scroll-text 是滚动的文本,通过设置 padding-left 为 100%,文本会比容器宽度多出一倍的空间,从而实现循环滚动的效果。@keyframes scroll-left 定义了从左至右的平滑滚动动画。