react实现popup中间弹出内容点击遮罩层关闭

import React, { useState } from 'react'
import styles from './index.module.scss'
export default function PopupCnter() {
  const [state, setState] = useState(true)
  const handleClick = (e) => {
    setState(false)
  }
  return (
    <div>
      <div className={styles.root}>
        {/* 容器 */}
        <div
          className={`container ${state ? 'active' : ''}`}
          onClick={handleClick}
        >
          <div className="Popup" onClick={(e) => e.stopPropagation()}>
            <h2>React ni hao nihao nan a ! wohao nanshou a </h2>
          </div>
        </div>
      </div>
    </div>
  )
}

----------------------------css 部分----------------------------

.root {
  * {
    box-sizing: border-box;
  }
  :global {
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 0;
    min-height: 100vh;
    .container {
      position: fixed;
      top: 0;
      right: 0;
      left: 0;
      bottom: 0;
      background-color: rgba(0, 0, 0, 0.3);
      justify-content: center;
      align-items: center;
      //   display: flex;
      display: none;
    }
    .container.active {
      display: flex;
    }
    .Popup {
      width: 400px;
      height: 200px;
      padding: 20px;
      background-color: white;
      border-radius: 6px;
      box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.3);
      position: relative;
    }
  }
}

### 实现Python流星雨特效 #### 使用Turtle库创建简单流星雨效果 对于初学者来说,`turtle` 库提供了一种直观的方式来进行图形编程。下面展示了一个利用 `turtle` 创建基本流星雨动画的例子: ```python import turtle import random def draw_star(x, y): star = turtle.Turtle() star.speed(0) star.penup() star.goto(x, y) star.pendown() size = random.randint(5, 15) for _ in range(5): star.forward(size) star.backward(size) star.left(72) def create_meteor_shower(): screen = turtle.Screen() screen.bgcolor("black") while True: x = random.randint(-screen.window_width()//2, screen.window_width()//2) y = screen.window_height()//2 color = (random.random(), random.random(), random.random()) meteor = turtle.Turtle(shape="circle", visible=False) meteor.color(color) meteor.penup() meteor.setpos(x,y) meteor.showturtle() while meteor.ycor()>-(screen.window_height())//2 : meteor.right(random.uniform(-10, 10)) meteor.forward(random.uniform(8, 15)) meteor.clearstamps() if random.choice([True,False]): draw_star(meteor.xcor()+random.randint(-10,10),meteor.ycor()-random.randint(0,30)) meteor.hideturtle() meteor.reset() create_meteor_shower() ``` 这段程序定义了两个主要部分:一个是用于画星星的小函数 `draw_star()`;另一个是主循环 `create_meteor_shower()` 来模拟流星划过夜空的效果[^2]。 #### 利用Pygame构建更复杂的流星雨场景 如果想要制作更加复杂和互动性强的流星雨游戏,则可以考虑采用 `pygame` 这样的多媒体模块。这里给出一段简化版的代码片段作为起点: ```python import pygame import sys from math import sin, cos, radians import random class Meteor(pygame.sprite.Sprite): def __init__(self): super().__init__() self.image = pygame.Surface((10, 10)) # 可替换为实际图片资源加载 self.rect = self.image.get_rect(center=(random.randrange(WIDTH), -20)) self.angle = random.uniform(radians(-45), radians(45)) self.velocity_x = int(cos(self.angle)*random.uniform(3, 9)) self.velocity_y = int(sin(self.angle)*random.uniform(3, 9)+gravity) def update(self): global score self.rect.centery += self.velocity_y self.rect.centerx += self.velocity_x if not (-20 < self.rect.top < HEIGHT or WIDTH+20 > self.rect.left > -20): meteors.remove(self) pygame.init() size = WIDTH, HEIGHT = 640, 480 screen = pygame.display.set_mode(size) clock = pygame.time.Clock() meteors = pygame.sprite.Group() gravity = 0.5 score = 0 while True: clock.tick(30) for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() new_meteors_count = min(int(score / 10) + 1, 5) for i in range(new_meteors_count): meteors.add(Meteor()) meteors.update() screen.fill((0, 0, 0)) meteors.draw(screen) pygame.display.flip() ``` 此段代码展示了如何使用面向对象的方法来管理多个流星实例,并让它们按照一定规律移动并最终消失于屏幕之外[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值