Cookie的选项卡加拖拽
<!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>Document</title>
<style>
div {
width: 100px;
height: 100px;
position: absolute;
left: 100px;
top: 100px;
background: red;
}
</style>
</head>
<body>
<div></div>
</body>
<script>
var div = document.querySelector('div');
if (getCookie('mmm')) {
div.style.left = getCookie('mmm').split(',')[0] + 'px'
div.style.top = getCookie('mmm').split(',')[1] + 'px'
}
div.onmousedown = function (e) {
var dx = e.pageX - div.offsetLeft
var dy = e.pageY - div.offsetTop
document.onmousemove = function (e) {
var x = e.pageX - dx;
var y = e.pageY - dy;
div.style.left = x + 'px'
div.style.top = y + 'px'
setCookie('mmm', [x, y], 1000)
}
document.onmouseup = function () {
document.onmousemove = null;
document.onmouseup = null;
}
}
//添加cookie
function setCookie(name, value, time) {
var date = new Date();
date.setDate(date.getDate() + time);
document.cookie = name + "=" + value + ";expires=" + date;
}
//获得cookie
function getCookie(name) {
var arr = document.cookie.split(";");
for (var i = 0; i < arr.length; i++) {
var arr2 = arr[i].split("=");
if (arr2[0] == name) {
return arr2[1];
}
}
return null;
}
//删除cookie
function removeCookie(name) {
setCookie(name, "", 0)
}
</script>
</html>
下面是Cookie的选项卡
<!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>Document</title>
<style>
* {
margin: 0;
padding: 0;
list-style: none;
}
ul {
display: flex;
}
ul li {
width: 100px;
height: 20px;
border: 1px solid #000;
}
ol li {
width: 405px;
height: 300px;
border: 1px solid #000;
display: none;
}
.uli {
background: red;
}
.oli {
background: red;
display: block;
}
</style>
</head>
<body>
<div>
<ul>
<li class="uli"></li>
<li></li>
<li></li>
<li></li>
</ul>
<ol>
<li class="oli">1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ol>
</div>
<script>
var uli = document.querySelectorAll('ul li')
var oli = document.querySelectorAll('ol li')
function bb(index) {
for (let j = 0; j < uli.length; j++) {
uli[j].className = '';
oli[j].className = '';
}
uli[index].className = 'uli'
oli[index].className = 'oli'
}
if (getCookie('xxk')) {
bb(getCookie('xxk') - 0)
}
for (let i = 0; i < uli.length; i++) {
uli[i].index = i;
uli[i].onclick = function () {
bb(i)
setCookie('xxk', this.index, 1000)
}
}
//添加cookie
function setCookie(name, value, time) {
var date = new Date();
date.setDate(date.getDate() + time);
document.cookie = name + "=" + value + ";expires=" + date;
}
//获得cookie
function getCookie(name) {
var arr = document.cookie.split(";");
for (var i = 0; i < arr.length; i++) {
var arr2 = arr[i].split("=");
if (arr2[0] == name) {
return arr2[1];
}
}
return null;
}
//删除cookie
function removeCookie(name) {
setCookie(name, "", 0)
}
</script>
</html>
先看后赞再关已成习惯谢谢!