前端的小玩意(8)——旋转的盒子(纯CSS动画效果)+可自由拖动(js)
来源:互联网 发布:淘宝买东西往哪里存钱 编辑:程序博客网 时间:2024/06/06 08:46
效果是一个旋转的盒子,他会展开六个面,展开结束后,会旋转,鼠标移动上去会变色;
demo网址:
http://jianwangsan.cn/boxDemo
HTML代码:
<div id="Box_Background" class="container"><p>这个盒子可以拖动</p> <div id="Box"> <div class="Box"> <div class="surface top">Top</div> <div class="surface bottom">Bottom</div> <div class="surface left">Left</div> <div class="surface right">Right</div> <div class="surface back">Back</div> <div class="surface front">Front</div> </div> </div></div>
CSS代码:(注:我在demo里使用了bootsrap的.container这个类,以使其居中
#Box_Background { position: relative; height: 300px; border: 1px solid #aaa; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none;}#Box { perspective: 500px; /*透视距离是500px,值越大透视越明显,但这个不是z轴上的移动,不会影响图片的大小*/ perspective-origin: 50% 50%; /*透视角度,居中*/ position: absolute; top: 100px; left: 50%; margin-left: -50px; /*这行和上一行用于居中设置*/ width: 100px; height: 100px; cursor: pointer;}a[href='/boxDemo'] { color: #555; text-decoration: none; background-color: #e5e5e5; -webkit-box-shadow: inset 0 3px 8px rgba(0, 0, 0, 0.125); -moz-box-shadow: inset 0 3px 8px rgba(0, 0, 0, 0.125); box-shadow: inset 0 3px 8px rgba(0, 0, 0, 0.125);}.Box { width: 100px; height: 100px; text-align: center; transform-style: preserve-3d; /*设置子元素是否继承3d,这样写是true*/}.Box .surface { position: absolute; border: 1px solid black; width: 100px; height: 100px; line-height: 100px; text-align: center;}@keyframes top { /*keyframes开头的都是动画,deg是度*/ 0% { } 100% { transform: rotateX(90deg) translateZ(50px) }}@keyframes bottom { 0% { } 100% { transform: rotateX(-90deg) translateZ(50px) }}@keyframes left { 0% { } 100% { transform: rotateY(-90deg) translateZ(50px) }}@keyframes right { 0% { } 100% { transform: rotateY(90deg) translateZ(50px) }}@keyframes front { 0% { } 100% { transform: translateZ(50px) }}@keyframes back { 0% { } 100% { transform: translateZ(-50px) rotateY(180deg) }}@keyframes all { 0% { } 100% { transform: rotate3d(1, 1, 1, 360deg) }}.Box .top { animation: top 1s both; /*动画名,动画时间,动画开始结束状态保持,下面有第四个参数是延迟播放时间*/}.Box:hover .top { background: red;}.Box .bottom { animation: bottom 1s both 1s;}.Box:hover .bottom { background: blue;}.Box .left { animation: left 1s both 2s;}.Box:hover .left { background: green;}.Box .right { animation: right 1s both 3s;}.Box:hover .right { background: yellow;}.Box .back { animation: back 1s both 4s;}.Box:hover .back { background: pink;}.Box .front { animation: front 1s both 5s;}.Box:hover .front { background: GreenYellow;}.Box { animation: all 3s both infinite 6s linear;}
拖动的JS代码:
$(document).ready(function () { var move = false; //移动标志,true为可以移动 var mouseX, mouseY, boxX, boxY; $("#Box").mousedown(function (evt) { mouseX = evt.clientX; //这里的值是鼠标坐标 mouseY = evt.clientY; boxX = parseInt($(this).css("margin-left")); //只获取px之前的数字 boxY = parseInt($(this).css("margin-top")); move = true; }) $("#Box").mouseup(function (evt) { move = false; }) $("#Box_Background").mousemove(function (evt) { if (!evt.buttons) { // 假如鼠标不是按下状态,那么取消移动,然后返回;因为在移动中鼠标取消按下,无法触发mouseup事件 move = false; return; } var back = $("#Box_Background")[0]; //限制盒子离开目标区域(实际上没有完全限制,会最多出去一半。因此可以更完美,只是我没写) if (evt.clientX < back.offsetLeft) { evt.clientX = back.offsetLeft } else if (evt.clientX > back.offsetLeft + back.clientWidth) { evt.clientX = back.offsetLeft + back.clientWidth; } else if (evt.clientY < back.offsetTop) { evt.clientY = back.offsetTop; } else if (evt.clientY > back.offsetTop + back.clientHeight) { evt.clientY = back.offsetTop + back.clientHeight; } // 这里是修改margin-left和margin-top的值来达到移动盒子的效果 if (move) { $("#Box").css("margin-left", boxX + (evt.clientX - mouseX) + 'px'); $("#Box").css("margin-top", boxY + (evt.clientY - mouseY ) + 'px'); } })})
0 0
- 前端的小玩意(8)——旋转的盒子(纯CSS动画效果)+可自由拖动(js)
- 前端的小玩意(15)——一步一步仿写三个图标和动画
- 前端的小玩意(6)——使用padding的左定宽、右自适应布局
- 前端的小玩意(13)——获取DOM的位置相关信息
- 前端的小玩意(2)jQuery的选择器大全
- 前端的小玩意(10)CSS3的:nth选择器
- 前端的小玩意(7)——自动给每个字下方加着重号
- 前端的小玩意(16)——利用setter和getter实现数据校验
- View的自由拖动(属性动画的实践之一)
- 前端的小玩意(3)禁止元素被选中
- 前端模型--css动画(旋转八音盒)
- 前端的小玩意(9.1)——做一个仿360工具箱的web页面(Tab按钮切换)
- 前端的小玩意(5)——用dojo写的二级下拉菜单自动添加功能
- 前端的小玩意(4)自定义checkbox或者radio的外观
- 前端的小玩意(11)上传文件和上传文件的后缀名限制
- js盒子抛物线移动(购物车动画效果)
- 前端的小玩意(1)input 只允许输入整数 + 用逗号进行三位分割
- 鼠标悬停旋转的图标按钮特效(纯CSS)
- mongodb复制和分片
- POJ2689 Prime distance 素数
- C#访问Access完整增删改查代码
- java开发乱码解决
- HTML5(李炎恢)学习笔记四 ------------- HTML5元素(中)
- 前端的小玩意(8)——旋转的盒子(纯CSS动画效果)+可自由拖动(js)
- marquee标签的向上无缝滚动
- Linux系统编程之信号的可重入性
- 【bzoj2002】【Hnoi2010】【Bounce】【弹飞绵羊】【分块】
- @ManyToMany 映射
- mybatis基础知识——学习笔记(1)
- Java ClassLoader深入解析(一)
- 注解基础
- 小算法---寻找最小的不能由n个数选取求和的数