[第七季]6.实现DIV的显示和隐藏

本文档通过实例演示了如何使用jQuery实现DIV元素的显示、隐藏及尺寸动态变化。包括了链式操作、动画效果、回调函数等知识点,并探讨了布局调整与元素分离的技术细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

4.复习笔记(这个就是课后习题以及课程所呈现的需求)

①让一个500*500的div进行显示和隐藏,还有高宽进行变换,测试分开变换和链式变换的区别.
②让一个300*300的div进行显示和隐藏,然后用标签进行显示.

5.自测代码

2.课堂笔记

(1)测试按钮id=btn,一个div id=div1,设置div1的背景颜色是红色,高度和宽度都是10px
(2)给btn打开的就绑定一个函数,让div1动态变化为高度和宽度动态变化为500,第二种方法是用链式操作的方法,但是这个方法有点不一样,他是分先后顺序的
<style type="text/css">
    #div1
    {
        height:10px;
        width:10px;
        background-color:red;
    }
</style>

<script type="text/javascript">

    $(function(){
        $("#btn").click(function(){
            //$("#div1").animate({width:500,height:500},4000);
            $("#div1").animate({height:300},4000).animate({width:300},2000);
        });
    });
</script>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>6.实现DIV的显示和隐藏</title>
</head>

<body>
<input type="button" value="测试" id="btn" />
<div id="div1">
</div>
</body>
</html>
2.按钮后标签也跟着显示
(1)新建一个html,设置一个btn,一个div=info=正在显示
新建一个div=div1=长宽为300,颜色为红色.
(2)btn和info(就是上面一行字)设置为浮动(让这两个元素进行浮动).
(3)这个时候,div和上面的btn以及info都杂糅在一起了,想要把div1分开,就要设置div1的clear属性,使其周围不能有浮动元素,分开之后仍然不满意,想要分开

20px,但是百度发现clear和margin-属性是矛盾的.
(4)直接设置btn和info边距为20,显示出了效果.
clear会和margin冲突,重置为零
http://www.cnblogs.com/rainman/archive/2008/05/07/1186932.html
<style type="text/css">
    #div1
    {
        width:300px;
        height:300px;
        background-color:red;
        clear:both;
        <!--使用了clear,那么margin-top就会失效-->

    }
    #btn,info
    {
        float:left;
        margin-bottom:20px;
    }

    #info
    {
        margin-left:20px;
    }
</style>
(5)回调函数的应用,我还真是不大懂
<script type="text/javascript">
    $(function(){
        $("#btn").click(function(){
            if($("#div1").css("display")=="none"){
                $("#info").html("正在显示......");
                $("#div1").show(1000,function(){
                    //这个函数叫做回调函数,
                    //也就是这个函数调用完成之后
                    //他会回来调用这个函数.
                    $("#info").html("显示完毕......");
                });

            }
            else
            {
                $("#info").html("正在隐藏......");
                $("#div1").hide(1000,function(){
                    $("#info").html("隐藏完毕......");
                });
            };
        });
    });


</script>

3.课程效果图

这里写图片描述

这里写图片描述

这里写图片描述

1.代码

<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>关爱展厅</title> <script src="https://cdn.tailwindcss.com"></script> <link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet"> <style> body { background: #0f172a; min-height: 100vh; display: flex; justify-content: center; align-items: center; font-family: 'Inter', sans-serif; overflow: hidden; } .gallery-container { perspective: 1000px; width: 100%; height: 100vh; display: flex; justify-content: center; align-items: center; } .carousel { position: relative; width: 300px; height: 400px; transform-style: preserve-3d; /* 使用更平滑的过渡函数 */ transition: transform 0.6s cubic-bezier(0.23, 1, 0.32, 1); } .carousel-item { position: absolute; width: 100%; height: 100%; backface-visibility: hidden; border-radius: 12px; overflow: hidden; box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3); /* 使用will-change优化性能 */ will-change: transform, box-shadow; transition: transform 0.5s cubic-bezier(0.23, 1, 0.32, 1), box-shadow 0.5s cubic-bezier(0.23, 1, 0.32, 1); cursor: pointer; z-index: 5; } .carousel-item:hover { box-shadow: 0 15px 40px rgba(0, 0, 0, 0.5); transform: translateZ(calc(400px + 10px)); /* 悬停时向前移动 */ z-index: 20; /* 确保悬停项在最上层 */ } .carousel-item:active { transform: translateZ(calc(400px)) scale(0.98); transition-duration: 0.2s; } .carousel-item img { width: 100%; height: 100%; object-fit: cover; /* 使用will-change优化性能 */ will-change: transform; transition: transform 0.7s cubic-bezier(0.23, 1, 0.32, 1); } .carousel-item:hover img { transform: scale(1.05); } .carousel-info { position: absolute; bottom: 0; left: 0; right: 0; padding: 20px; background: linear-gradient(to top, rgba(0, 0, 0, 0.8) 0%, rgba(0, 0, 0, 0) 100%); transform: translateY(20px); opacity: 0; /* 使用will-change优化性能 */ will-change: transform, opacity; transition: transform 0.4s cubic-bezier(0.23, 1, 0.32, 1), opacity 0.4s cubic-bezier(0.23, 1, 0.32, 1); z-index: 30; /* 提高信息卡片的z-index */ } .carousel-item:hover .carousel-info { transform: translateY(0); opacity: 1; } .carousel-title { font-size: 1.5rem; font-weight: bold; margin-bottom: 5px; color: white; } .carousel-desc { font-size: 0.9rem; color: rgba(255, 255, 255, 0.8); } .controls { position: fixed; bottom: 50px; left: 50%; transform: translateX(-50%); display: flex; gap: 20px; z-index: 20; } .control-btn { background: rgba(255, 255, 255, 0.1); color: white; border: none; padding: 10px 20px; border-radius: 5px; cursor: pointer; /* 使用will-change优化性能 */ will-change: background; transition: background 0.3s ease; } .control-btn:hover { background: rgba(255, 255, 255, 0.2); } #activityBtn { position: fixed; top: 20px; right: 20px; background: rgba(255, 255, 255, 0.1); color: white; border: none; padding: 10px 20px; border-radius: 5px; cursor: pointer; /* 使用will-change优化性能 */ will-change: background; transition: background 0.3s ease; z-index: 20; } #activityBtn:hover { background: rgba(255, 255, 255, 0.2); } #activityModal { display: none; position: fixed; z-index: 100; left: 0; top: 0; width: 100%; height: 100%; overflow: auto; background-color: rgba(0, 0, 0, 0.7); /* 使用will-change优化性能 */ will-change: opacity; transition: opacity 0.4s cubic-bezier(0.23, 1, 0.32, 1); } .modal-content { background-color: #1e293b; margin: 10% auto; padding: 20px; border-radius: 10px; width: 80%; max-width: 600px; color: white; box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3); /* 使用will-change优化性能 */ will-change: transform, opacity; transform: translateY(20px); opacity: 0; transition: transform 0.5s cubic-bezier(0.23, 1, 0.32, 1), opacity 0.5s cubic-bezier(0.23, 1, 0.32, 1); } .modal-open .modal-content { transform: translateY(0); opacity: 1; } .close { color: #aaa; float: right; font-size: 28px; font-weight: bold; /* 使用will-change优化性能 */ will-change: color; transition: color 0.3s ease; } .close:hover, .close:focus { color: white; text-decoration: none; cursor: pointer; } .activity-list { margin-top: 20px; } .activity-item { padding: 15px 0; border-bottom: 1px solid rgba(255, 255, 255, 0.1); /* 使用will-change优化性能 */ will-change: background; transition: background 0.2s ease; } .activity-item:hover { background: rgba(255, 255, 255, 0.03); } .activity-item:last-child { border-bottom: none; } .activity-date { font-weight: bold; color: #94a3b8; } @media (max-width: 600px) { .carousel { width: 250px; height: 350px; } .modal-content { width: 90%; margin: 5% auto; } #activityBtn { top: 10px; right: 10px; padding: 8px 15px; font-size: 0.9rem; } } </style> </head> <body> <div class="gallery-container"> <div id="carousel" class="carousel"> <!-- 图片将通过JS动态添 --> </div> </div> <div class="controls"> <button id="prevBtn" class="control-btn"> <i class="fa fa-arrow-left mr-2"></i>上一张 </button> <button id="nextBtn" class="control-btn"> 下一张<i class="fa fa-arrow-right ml-2"></i> </button> </div> <button id="activityBtn" class="control-btn"> <i class="fa fa-calendar mr-2"></i>活动清单 </button> <div id="activityModal" class="modal"> <div class="modal-content"> <span class="close">×</span> <h2 class="text-xl font-bold mb-4">7月活动清单</h2> <div class="activity-list"> <!-- 活动列表将通过JS动态生成 --> </div> </div> </div> <script> document.addEventListener('DOMContentLoaded', () => { const carousel = document.getElementById('carousel'); const prevBtn = document.getElementById('prevBtn'); const nextBtn = document.getElementById('nextBtn'); const activityBtn = document.getElementById('activityBtn'); const activityModal = document.getElementById('activityModal'); const closeBtn = document.querySelector('.close'); const itemsCount = 5; // 更新为5个项目 const radius = 400; let currentAngle = 0; const angleStep = 360 / itemsCount; // 重新计算角度步长 // 优化后的主题数据,添了"新员工融入""任务分派" const communicationRecords = [ { id: 1001, title: "员工生病探望", participants: ["人力资源部: 王经理", "部门主管: 张主管"], content: "前往医院探望生病住院的员工,带去公司关怀慰问品,了解康复情况并协调工作安排。", time: "2023-06-15", location: "市中心医院", followUp: "人力资源部持续跟进员工康复进度,部门安排同事临时接手相关工作。", topic: "生病看望", imageId: 1005 // 医疗相关图片 }, { id: 1003, title: "6月员工生日会", participants: ["行政部: 刘主管", "6月寿星: 全体成员"], content: "举办6月员工生日会,庆祝员工生日,分享蛋糕礼物,增强团队凝聚力。", time: "2023-06-25", location: "公司会议室", followUp: "行政部收集员工反馈,持续优化生日关怀活动形式。", topic: "生日关怀", imageId: 1003 // 庆祝相关图片 }, { id: 1004, title: "端午节福利发放", participants: ["行政部: 全体成员", "各部门代表"], content: "为全体员工发放端午节福利,包括粽子、咸鸭蛋等传统节日礼品,表达节日问候。", time: "2023-06-22", location: "公司大厅", followUp: "行政部持续关注员工福利需求,准备其他节日福利方案。", topic: "节日福利", imageId: 1004 // 食物相关图片 }, { id: 1005, title: "新员工入职欢迎会", participants: ["人力资源部: 李主管", "新员工: 张明、王芳", "部门同事代表"], content: "为新员工举办入职欢迎会,介绍公司文化、规章制度,帮助新员工快速融入团队。", time: "2023-06-30", location: "公司培训室", followUp: "各部门安排导师指导新员工,定期跟进新员工适应情况。", topic: "新员工融入", imageId: 1006 // 团队合作相关图片 }, { id: 1006, title: "项目任务分派会议", participants: ["项目经理: 赵经理", "开发团队: 全体成员", "测试团队: 负责人"], content: "召开项目任务分派会议,明确各成员职责任务,制定项目计划时间节点。", time: "2023-07-05", location: "公司会议室B", followUp: "定期召开项目进度会议,及时解决遇到的问题,确保项目顺利进行。", topic: "任务分派", imageId: 1008 // 商务会议相关图片 } ]; // 更新活动列表,添新员工融入任务分派相关活动 const currentMonthActivities = [ { date: "2023-07-05", title: "新一期员工入职培训", type: "培训", location: "公司培训室", participants: "人力资源部、新入职员工" }, { date: "2023-07-10", title: "员工户外拓展训练", type: "团队活动", location: "城市郊外拓展基地", participants: "全体员工" }, { date: "2023-07-15", title: "客户满意度调研分析", type: "分析会", location: "公司会议室A", participants: "市场部、客服部" }, { date: "2023-07-20", title: "第三度项目任务分派", type: "任务分配", location: "公司大会议室", participants: "各部门负责人、项目团队" }, { date: "2023-07-25", title: "产品创新头脑风暴", type: "研讨会", location: "公司创意空间", participants: "产品部、研发部、设计部" } ]; function createCarouselItems() { for (let i = 0; i < itemsCount; i++) { const angle = (i * angleStep) * (Math.PI / 180); const item = document.createElement('div'); item.className = 'carousel-item'; item.style.transform = `rotateY(${i * angleStep}deg) translateZ(${radius}px)`; item.dataset.index = i; item.dataset.topic = communicationRecords[i].topic; const img = document.createElement('img'); img.src = `https://picsum.photos/id/${communicationRecords[i].imageId}/600/800`; img.alt = communicationRecords[i].title; const info = document.createElement('div'); info.className = 'carousel-info'; const title = document.createElement('h3'); title.className = 'carousel-title'; title.textContent = communicationRecords[i].title; const desc = document.createElement('p'); desc.className = 'carousel-desc'; desc.textContent = communicationRecords[i].participants.join('、'); info.appendChild(title); info.appendChild(desc); item.appendChild(img); item.appendChild(info); carousel.appendChild(item); item.addEventListener('click', () => { const topic = encodeURIComponent(communicationRecords[i].topic); console.log(`跳转到主题: ${topic}`); item.style.transform = `rotateY(${i * angleStep}deg) translateZ(${radius - 10}px) scale(0.98)`; setTimeout(() => { item.style.transform = `rotateY(${i * angleStep}deg) translateZ(${radius}px)`; // 注意:这里使用了相对路径,实际使用时请根据你的项目结构调整 window.location.href = `./photo-wall.html?topic=${topic}`; }, 150); }); } } function createActivityList() { const activityList = document.querySelector('.activity-list'); activityList.innerHTML = ''; currentMonthActivities.forEach(activity => { const activityItem = document.createElement('div'); activityItem.className = 'activity-item'; const dateElement = document.createElement('div'); dateElement.className = 'activity-date'; dateElement.textContent = activity.date; const titleElement = document.createElement('h3'); titleElement.className = 'text-lg font-semibold mt-1'; titleElement.textContent = activity.title; const typeElement = document.createElement('div'); typeElement.className = 'text-sm text-blue-400 mt-1'; typeElement.textContent = activity.type; const locationElement = document.createElement('div'); locationElement.className = 'text-sm text-gray-300 mt-1'; locationElement.innerHTML = `<i class="fa fa-map-marker mr-1"></i> ${activity.location}`; const participantsElement = document.createElement('div'); participantsElement.className = 'text-sm text-gray-300 mt-1'; participantsElement.innerHTML = `<i class="fa fa-users mr-1"></i> ${activity.participants}`; activityItem.appendChild(dateElement); activityItem.appendChild(titleElement); activityItem.appendChild(typeElement); activityItem.appendChild(locationElement); activityItem.appendChild(participantsElement); activityList.appendChild(activityItem); }); } function rotateCarousel(angle) { // 使用requestAnimationFrame优化动画性能 requestAnimationFrame(() => { carousel.style.transform = `rotateY(${angle}deg)`; }); } prevBtn.addEventListener('click', () => { currentAngle += angleStep; rotateCarousel(currentAngle); }); nextBtn.addEventListener('click', () => { currentAngle -= angleStep; rotateCarousel(currentAngle); }); let isDragging = false; let startX, startRotation; let lastAnimationFrameId = null; document.addEventListener('mousedown', (e) => { if (!e.target.closest('.carousel-item')) { isDragging = true; startX = e.clientX; startRotation = currentAngle; document.body.style.cursor = 'grabbing'; // 防止拖动时选择文本 document.body.style.userSelect = 'none'; } }); document.addEventListener('mousemove', (e) => { if (!isDragging) return; const diffX = e.clientX - startX; const newAngle = startRotation - diffX * 0.5; // 使用requestAnimationFrame优化性能 if (lastAnimationFrameId) { cancelAnimationFrame(lastAnimationFrameId); } lastAnimationFrameId = requestAnimationFrame(() => { currentAngle = newAngle; rotateCarousel(currentAngle); }); }); document.addEventListener('mouseup', () => { if (isDragging) { isDragging = false; document.body.style.cursor = 'default'; document.body.style.userSelect = ''; if (lastAnimationFrameId) { cancelAnimationFrame(lastAnimationFrameId); } } }); activityBtn.addEventListener('click', () => { createActivityList(); activityModal.style.display = 'block'; // 添延迟以确保过渡效果生效 setTimeout(() => { activityModal.classList.add('modal-open'); }, 10); }); closeBtn.addEventListener('click', () => { activityModal.classList.remove('modal-open'); // 等待过渡完成后隐藏模态框 setTimeout(() => { activityModal.style.display = 'none'; }, 500); }); window.addEventListener('click', (e) => { if (e.target === activityModal) { activityModal.classList.remove('modal-open'); // 等待过渡完成后隐藏模态框 setTimeout(() => { activityModal.style.display = 'none'; }, 500); } }); createCarouselItems(); rotateCarousel(currentAngle); }); </script> </body> </html> 如何调整transform的值,使得所有的<div class="carousel-info"><h3 class="carousel-title">端午节福利发放</h3><p class="carousel-desc">行政部: 全体成员、各部门代表</p></div>都可以显示
最新发布
07-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值