web第三次作业

CSS样式

* {

margin: 0;

padding: 0;

box-sizing: border-box;

}

html,

body {

width: 100%;

height: 100%;

}

.container {

width: 100%;

height: 100%;

background-color: #f2f1f2;

}

header {

width: 1200px;

height: 50px;

background-color: #fff;

margin: 0 auto;

display: flex;

justify-content: space-between;

align-items: center;

}

header div:nth-of-type(2) {

display: flex;

gap: 20px;

cursor: pointer;

}

header div:nth-of-type(2) span:hover {

font-weight: bolder;

color: red;

}

.login-box {

display: none;

overflow: hidden;

width: 500px;

height: 300px;

background-color: #1fdec1;

border: solid 1px orangered;

border-radius: 8px;

box-shadow: rgba(255, 0, 0, 0.5) 5px 5px 5px;

position: absolute;

left: 1150px;

top: 50px;

}

.login-box .header {

height: 40px;

background-color: orangered;

display: flex;

justify-content: space-between;

align-items: center;

color: white;

cursor: pointer;

padding: 0 10px;

}

HTML

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Document</title>

</head>

<body>

<div class="container">

<header>

<div>

<span>登录后享受更多内容</span>

</div>

<div>

<span id="login">登录</span>

<span id="register">注册</span>

</div>

<div class="login-box" id="login-box">

<div class="header" id="header">

<span>登录</span>

<span id="close">X</span>

</div>

</div>

</header>

</div>

</body>

</html>

JS代码

<script>

let _login = document.getElementById("login");

let _login_box = document.getElementById("login-box");

_login.onclick = function () {

_login_box.style.display = "block";

}

let _close = document.getElementById("close");

_close.onclick = function () {

_login_box.style.display = "none";

}

let _header = document.getElementById("header");

document.onmousedown = function (event) {

let offsetX = event.offsetX;

let offsetY = event.offsetY;

_header.onmousemove = function (event2) {

let mouseX = event2.clientX;

let mouseY = event2.clientY;

let loginX = mouseX - offsetX + "px";

let loginY = mouseY - offsetY + "px";

if (loginX <= 0) {

loginX = 0;

}

if (loginY <= 0) {

loginY = 0;

}

let iw = window.innerWidth;

let lw = getComputedStyle(_login_box).width;

lw = parseInt(lw);

if (loginX >= (iw - lw)) {

loginX = iw - lw;

}

let ih = window.innerHeight;

let lh = getComputedStyle(_login_box).height;

lh = parseInt(lh);

if (loginY >= (ih - lh)) {

loginY = ih - lh;

}

_login_box.style.left = loginX;

_login_box.style.top = loginY;

}

}

document.onmouseup = function() {

_header.onmousemove = null;

}</script>

相关推荐
范文杰7 分钟前
AI 时代如何更高效开发前端组件?21st.dev 给了一种答案
前端·ai编程
拉不动的猪15 分钟前
刷刷题50(常见的js数据通信与渲染问题)
前端·javascript·面试
拉不动的猪24 分钟前
JS多线程Webworks中的几种实战场景演示
前端·javascript·面试
FreeCultureBoy1 小时前
macOS 命令行 原生挂载 webdav 方法
前端
uhakadotcom2 小时前
Astro 框架:快速构建内容驱动型网站的利器
前端·javascript·面试
uhakadotcom2 小时前
了解Nest.js和Next.js:如何选择合适的框架
前端·javascript·面试
uhakadotcom2 小时前
React与Next.js:基础知识及应用场景
前端·面试·github
uhakadotcom2 小时前
Remix 框架:性能与易用性的完美结合
前端·javascript·面试
uhakadotcom2 小时前
Node.js 包管理器:npm vs pnpm
前端·javascript·面试
LaoZhangAI3 小时前
2025最全GPT-4o图像生成API指南:官方接口配置+15个实用提示词【保姆级教程】
前端