案例一
盒子拖拽案例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>拖拽案例</title>
<style>
* {
padding:0px;
margin:0px
}
.login {
width: 300px;
height:300px;
background: pink;
position: absolute;
}
.login .header {cursor:move;width:100%;height:100px;background: black;}
</style>
</head>
<body>
<div class="login">
<div class="header"></div>
</div>
</body>
<script>
var headero=document.querySelector(".header")
//1. header绑定按下事件
headero.onmousedown=function(e){
var olde=e||window.event
//2. 事件处理函数中 给整个网页绑定移动事件
document.onmousemove=function(e){
var e=e||window.event
//3. 移动事件处理函数中 1获取坐标 2给login设置
var logino=document.querySelector(".login")
logino.style.left=e.pageX-olde.offsetX+"px" //用浏览器窗口左上角为原点的位置 减去 以header左上角为原点的位置来计算鼠标点击的位置
logino.style.top=e.pageY-olde.offsetY+"px" //同理
}
//n. 网页鼠标松开 取消移动
headero.onmouseup=function(){
document.onmousemove=null
}
}
</script>
</html>
注意点:
1、想要按住黑色区域内任意位置拖动整个方块,需要用
e.pageX-olde.offsetX+“px”
用浏览器窗口左上角为原点的位置 减去 以header左上角为原点的位置来计算鼠标点击的位置
2、鼠标松开便不再控制方块位置,要重新给header绑上onmouseout事件,然后在事件处理函数中将整个网页(document)的移动事件(onmousemove)移除
即:
headero.οnmοuseοut=function(){
document.οnmοusemοve=null
}
案例二
css3完成简单的线性运动动画效果(无限旋转,右移后恢复原位置再右移重复运动)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>css3实现线性运动动画</title>
<style>
*{
margin: 0;
padding: 0;
}
#box{
margin: 100px auto;
width: 200px;
height: 200px;
background: red;
background-size: cover;
}
.img{
width: 200px;
height: 200px;
}
#box span{
width: 40px;
height:60px;
background-color: black;
color: #fff;
line-height: 60px;
position: absolute;
top: 50%;
right: -40px;
margin-top: -30px;
}
/* 使用动画类(标签上使用这个类名就会有动画效果)
animateClass:自定义出来的动画类名
1s:运动速度,
linear:线性运动
infinite:无限的重复运动(加了此属性就会重复运动,不加就只运动一次结束)
*/
.dongAnin{
animation: animateClass 1s linear infinite;
}
/* 创建动画类 */
@keyframes animateClass {
from{
/* 向右移动 */
/* transform: translateX(0); */
/* 旋转 */
transform:rotate(0)
}
to{
/* 向右移动 */
/* transform: translateX(50%); */
/* 旋转 */
transform:rotate(360deg)
}
}
</style>
</head>
<body>
<div id="box" class="dongAnin">
</div>
</body>
<script >
</script>
</html>
案例三
可手动点击下一页上一页或自动播放的轮播图(原生js)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>轮播图案例</title>
<style>
* {
margin: 0;
padding: 0;
}
li {
list-style: none;
}
.big_box {
position: relative;
width: 500px;
height: 400px;
margin: 100px auto;
overflow: hidden;
}
.big_box ul {
position: absolute;
left: 0;
width: 600%;
height: 400px;
}
.big_box ul li {
float: left;
}
.big_box ul li img {
width: 500px;
height: 400px;
}
.left {
position: absolute;
top: 50%;
left: 0;
transform: translateY(-50%);
background-color: rgba(255, 255, 255, .5);
width: 30px;
height: 30px;
line-height: 30px;
text-align: center;
}
.right {
position: absolute;
top: 50%;
right: 0;
transform: translateY(-50%);
background-color: rgba(255, 255, 255, .5);
width: 30px;
height: 30px;
line-height: 30px;
text-align: center;
}
.big_box ol {
position: absolute;
bottom: 0;
/* 这里别给ol限制宽度,让小圆圈的个数自动把ol撑大 */
left: 50%;
transform: translateX(-50%);
height: 10%;
line-height: 10%;
text-align: center;
}
.big_box ol li {
float: left;
width: 20px;
height: 20px;
background-color: rgba(0, 0, 0, .5);
border-radius: 50%;
margin-left: 5px;
}
.big_box ol .current {
background-color: rgb(211, 29, 29);
}
.left,
.right,
ol li {
cursor: pointer;
}
</style>
</head>
<body>
<div class="big_box">
<!-- 轮播的图片 -->
<ul>
<li>
<img src="https://img-blog.csdnimg.cn/43255527f430420bad42be39a6f0e94d.png" alt="">
</li>
<li>
<img src="https://img-blog.csdnimg.cn/bff6932a9b224fa996b66ae6388590e6.png" alt="">
</li>
<li>
<img src="https://img-blog.csdnimg.cn/ad4631d47b364797bae98bc6f2ea4852.png" alt="">
</li>
</ul>
<!-- 左右按键 -->
<div class="left"><</div>
<div class="right">></div>
<!-- 小圆圈 -->
<ol>
</ol>
</div>
<script>
var timer;
var big_box = document.querySelector('.big_box');
var ul = big_box.querySelector('ul');
var ol = big_box.querySelector('ol');
var lis_img = ul.querySelectorAll('li');
var left = document.querySelector('.left');
var right = document.querySelector('.right');
var num = 0; //记录要滑到第几张图片
//封装运动函数
function animate(obj, target) {
var timer1 = setInterval(function () {
var current = obj.offsetLeft;
var step = 10;
step = current > target ? -step : step;
// 下面要包括等于的情况,否则会发生抖动
if (Math.abs(current - target) <= Math.abs(step)) {
clearInterval(timer1);
obj.style.left = target + 'px';
}
else {
obj.style.left = current + step + 'px';
}
}, 10)
}
//小圆圈样式改变
function circlechange(circles, circle) {
if (circle == lis_img.length) {
circle = 0;
}
//排他思想设置小圆圈样式
//排他思想第一步:先把所有的小圆圈样式去掉
for (var i = 0; i < circles.length; i++) {
circles[i].className = "";
}
//排他思想第二步:把当前图片对应的小圆圈设置样式
circles[circle].className = "current";
}
//在页面刚加载进来就执行代码
window.addEventListener('load', function () {
//设置小圆点的个数
for (var i = 0; i < lis_img.length; i++) {
var li = document.createElement('li');
ol.appendChild(li);
// 给小圆圈添加自定义属性
li.setAttribute('index', i);
//一开始第一个小圆圈就是被选中状态
if (i == 0) {
li.className = "current";
}
//给小圆圈添加点击处理事件
li.addEventListener('click', function () {
//排他思想实现小圆圈样式改变
for (var j = 0; j < ol.children.length; j++) {
ol.children[j].className = "";
}
this.className = "current";
//实现点击小圆圈后图片滑动
var index = this.getAttribute('index');
animate(ul, -index * big_box.offsetWidth);
// 在图片滑动的同时对应的小圆圈样式也要发生改变,所以调用animate函数同时调用circlechange函数
circlechange(circles, index);
})
}
//为了实现无缝衔接的切换图片,要把第一张图片克隆到最后一张图片的附近
var circles = ol.querySelectorAll('li');
// cloneNode函数若括号里面是true,则是深拷贝,false则是浅拷贝
var li_img = ul.children[0].cloneNode(true);
ul.appendChild(li_img);
//点击右箭头向右滑动
right.addEventListener('click', function () {
//下面if代码是实现向右滑动的无缝衔接,不懂的建议自己手动模拟一遍
//点击num+1,当num大于或等于图片的数量时就归零,好让轮播回到第一张图
if (num >= lis_img.length) {
num = 0;
//注意改变属性left的值后面一定要跟px,否则没有效果
ul.style.left = 0 + 'px';
}
num++;
animate(ul, -num * big_box.offsetWidth);
circlechange(circles, num);
})
//点击左箭头向左滑动
left.addEventListener('click', function () {
//下面if代码是实现向左滑动的无缝衔接,不懂的建议自己手动模拟一遍
if (num <= 0) {
num = lis_img.length;
ul.style.left = -lis_img.length * big_box.offsetWidth + 'px';
}
num--;
animate(ul, -num * big_box.offsetWidth);
circlechange(circles, num);
})
//实现自动播放----因为自动播放的功能和向右滑动的功能一样,所以直接调用向右滑动的函数
timer = setInterval(function () {
right.click();
}, 1000)
//鼠标放到盒子上停止自动播放
big_box.addEventListener('mouseover', function () {
clearInterval(timer);
})
//鼠标离开自动播放
big_box.addEventListener('mouseout', function () {
clearInterval(timer);
//在重新创建一个定时器时最好先清除一下定时器
timer = setInterval(function () {
right.click();
}, 1000)
})
})
window.addEventListener('onunload', function (){
console.log(99999);
clearInterval(timer);
})
</script>
</body>
</html>
效果图
案例四
跑马灯案例(原生js,默认是js实现的效果,内部也有只用css动画实现的代码默认被注释着的)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>向左匀速运动的走马灯效果</title>
<style>
.container {
box-sizing: border-box;
overflow: hidden;
position: relative;
width: 1000px;
height: 200px;
margin: 50px auto 0px;
border: 1px solid red;
}
.container .wrapper {
position: absolute;
top: 0px;
left: 0px;
box-sizing: border-box;
width: 2200px;
height: 200px;
}
.container .wrapper .slider {
box-sizing: border-box;
float: left;
width: 200px;
height: 200px;
padding: 10px 20px;
}
.container .wrapper .slider span {
display: block;
/* 不换行三件套 */
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.container .wrapper .slider img {
width: 150px;
height: 150px;
}
/* 这跑马灯的效果只用css动画也可实现,以下注释的css动画样式就是用css动画实现的代码 */
/* .container .wrapper {
animation: test 5s infinite linear;
}
@keyframes test {
from {
left: 0px;
}
to {
left: -1200px;
}
} */
.container:hover .wrapper {
animation-play-state: paused;
}
</style>
</head>
<body>
<div class="container ">
<div class="wrapper ">
<!-- 每一张图片slider -->
<div class="slider">
<img src="./img/1.jpg">
<span>天地壹号 清爽酸甜 苹果醋22222222222</span>
</div>
<div class="slider">
<span>天地壹号 清爽酸甜 苹果醋22222222222</span>
<img src="./img/2.jpg">
</div>
<div class="slider">
<img src="./img/3.jpg">
<span>天地壹号 清爽酸甜 苹果醋</span>
</div>
<div class="slider">
<span>天地壹号 清爽酸甜 苹果醋</span>
<img src="./img/4.jpg">
</div>
<!-- 无缝的 -->
<div class="slider">
<span>天地壹号 清爽酸甜 无缝的苹果醋</span>
<img src="./img/1.jpg">
</div>
<div class="slider">
<img src="./img/2.jpg">
<span>天地壹号 清爽酸甜 无缝的苹果醋</span>
</div>
<div class="slider">
<img src="./img/3.jpg">
<span>天地壹号 清爽酸甜 苹果醋</span>
</div>
<div class="slider">
<span>天地壹号 清爽酸甜 苹果醋</span>
<img src="./img/4.jpg">
</div>
</div>
</div>
</body>
<script>
//获取装照片的盒子
var wrapper = document.querySelector('.wrapper'),
timer = null,
step = -20,
left = 0;
//页面打开就开始加载动画
window.onload = function () {
timer = setInterval(autoMove, 100)
}
//鼠标移入清除定时器,停止运动
wrapper.addEventListener('mouseenter',function(){
clearInterval(timer)
})
//鼠标移出继续运动
wrapper.addEventListener('mouseleave',function(){
timer = setInterval(autoMove, 100)
})
//封装运动函数
function autoMove() {
//获取wrapper盒子往左边运动的距离
left = getCss(wrapper, 'left');
console.log(left);
left += step //100毫秒加-20(即每过100毫秒就往左运动10px)
if (left < -1200) {//当往左运动超过1200px时就将wrapper距离左边距离归零,过渡清零
wrapper.style.left = 0 + 'px';
wrapper.style.transition = 'all 0s'
left = getCss(wrapper, 'left');
}
wrapper.style.left = left + 'px';//往左运动不超过1200px就继续加
wrapper.style.transition = 'all .3s' //并添加0.3s的过渡
}
//封装获取元素css样式的方法
//getComputedStyle只能读取样式,不可以修改样式,会引起回流。
function getCss(culEle, attr) {
return parseFloat(window.getComputedStyle(culEle, null)[attr]);
}
</script>
</html>
效果图
案例五
css动画闪动的红点
<html>
<head>
<meta charset="utf-8">
<meta data-n-head="1" name="viewport" content="width=device-width, initial-scale=1">
<style type="text/css">
body {
display: flex;
align-items: center;
justify-content: center;
min-height: 100vh;
background-color: #1f3244;
}
.container {
width: 400px;
height: 400px;
}
.pulse {
background-color: coral;
height: 200px;
width: 200px;
border-radius: 100%;
position: relative;
}
.ring {
position: absolute;
background-color: inherit;
height: 100%;
width: 100%;
border-radius: 100%;
opacity: 0.8;
animation: pulsing 2s ease-out infinite;
}
.ring:nth-of-type(1) {
animation-delay: -0.5s;
}
.ring:nth-of-type(2) {
animation-delay: -1s;
}
.ring:nth-of-type(3) {
animation-delay: -1.5s;
}
@keyframes pulsing {
100% {
transform: scale(1.75);
opacity: 0;
}
}
</style>
</head>
<body>
<div class="container">
<div class="pulse">
<div class="ring"></div>
<div class="ring"></div>
<div class="ring"></div>
<div class="ring"></div>
</div>
</div>
</body>
</html>
效果图