2021-08-24 簡單的

1.打字游戲

<!DOCTYPE html>

<html lang='en'>

<head>

? ? <meta charset='UTF-8'>

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

? ? <meta http-equiv='X-UA-Compatible' content='ie=edge'>

? ? <title>Document</title>

? ? <style>

? ? ? ? span {

? ? ? ? ? ? width: 20px;

? ? ? ? ? ? height: 20px;

? ? ? ? ? ? background: red;

? ? ? ? ? ? text-align: center;

? ? ? ? ? ? line-height: 20px;

? ? ? ? ? ? border-radius: 50%;

? ? ? ? ? ? color: #ffffff;

? ? ? ? ? ? font-weight: 700;

? ? ? ? ? ? position: absolute;

? ? ? ? ? ? top: 0;

? ? ? ? ? ? left: 0;

? ? ? ? }

? ? </style>

</head>

<body>

</body>

<div>得分:

? ? <b></b>

</div>

<!--<span></span> -->

</html>

<script>

? ? var str = 'qwertyuiopasdfghjklzxcvbnm0123456789';

? ? var s = [];

? ? var timer;

? ? var score = 0;

? ? var span = document.createElement('span');

? ? function createSpan() {

? ? ? ? document.body.appendChild(span);

? ? ? ? var index = Math.floor(Math.random() * str.length);

? ? ? ? s = str[index];

? ? ? ? span.innerText = s;

? ? ? ? var x = Math.random() * (document.documentElement.clientWidth - span.offsetWidth);

? ? ? ? span.style.left = x + 'px'

? ? ? ? move()

? ? }

? ? createSpan();

? ? function move() {

? ? ? ? var num = 0;

? ? ? ? timer = setInterval(function () {

? ? ? ? ? ? num += 3

? ? ? ? ? ? if (num >= document.documentElement.clientHeight - span.clientHeight - 10) {

? ? ? ? ? ? ? ? document.body.removeChild(span);

? ? ? ? ? ? ? ? clearInterval(timer);

? ? ? ? ? ? ? ? createSpan()

? ? ? ? ? ? }

? ? ? ? ? ? span.style.top = num + 'px'

? ? ? ? }, 10);

? ? }

? ? document.onkeypress = function (e) {

? ? ? ? e = e || window.event;

? ? ? ? if (e.keyCode == s.charCodeAt(0)) {

? ? ? ? ? ? clearInterval(timer);

? ? ? ? ? ? createSpan()

? ? ? ? ? ? score++;

? ? ? ? ? ? document.querySelector('b').innerText = score;

? ? ? ? }

? ? }

</script>

2.元素拖拽

<!DOCTYPE html>

<html lang='en'>

<head>

? ? <meta charset='UTF-8'>

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

? ? <meta http-equiv='X-UA-Compatible' content='ie=edge'>

? ? <title>Document</title>

? ? <style>

? ? ? ? div {

? ? ? ? ? ? width: 100px;

? ? ? ? ? ? height: 100px;

? ? ? ? ? ? background: red;

? ? ? ? ? ? position: absolute;

? ? ? ? ? ? top: 0;

? ? ? ? ? ? left: 0;

? ? ? ? }

? ? </style>

</head>

<body>

? ? <div></div>

</body>

</html>

<script>

? ? var div = document.querySelector('div');

? ? div.onmousedown = function (e) {

? ? ? ? e = e || window.event;

? ? ? ? //獲取鼠標(biāo)在div元素中的位置

? ? ? ? var offX = e.offsetX;

? ? ? ? var offY = e.offsetY;

? ? ? ? document.onmousemove = function (e) {

? ? ? ? ? ? e = e || window.event;

? ? ? ? ? ? //獲取鼠標(biāo)在瀏覽器窗口中的實(shí)位置

? ? ? ? ? ? var mouseX = e.clientX;

? ? ? ? ? ? var mouseY = e.clientY;

? ? ? ? ? ? var y = mouseY - offY;

? ? ? ? ? ? var x = mouseX - offX;

? ? ? ? ? ? if (x <= 0) {

? ? ? ? ? ? ? ? x = 0;

? ? ? ? ? ? }

? ? ? ? ? ? if (y <= 0) {

? ? ? ? ? ? ? ? y = 0;

? ? ? ? ? ? }

? ? ? ? ? ? var maxWidth = document.documentElement.clientWidth - div.offsetWidth;

? ? ? ? ? ? var maxHeight = document.documentElement.clientHeight - div.offsetHeight;

? ? ? ? ? ? if (x >= maxWidth) {

? ? ? ? ? ? ? ? x = maxWidth

? ? ? ? ? ? };

? ? ? ? ? ? if (y >= maxHeight) {

? ? ? ? ? ? ? ? y = maxHeight

? ? ? ? ? ? }

? ? ? ? div.style.top = y + 'px';

? ? ? ? div.style.left = x + 'px';

? ? ? ? }/*? */

? ? }

? ? div.onmouseup = function (e) {

? ? ? ? document.onmousemove = ''

? ? }

</script>


3.進(jìn)度條設(shè)置

<!DOCTYPE html>

<html lang='en'>

<head>

? ? <meta charset='UTF-8'>

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

? ? <meta http-equiv='X-UA-Compatible' content='ie=edge'>

? ? <title>Document</title>

? ? <style>

? ? ? ? .box1 {

? ? ? ? ? ? width: 500px;

? ? ? ? ? ? height: 50px;

? ? ? ? ? ? position: relative;

? ? ? ? ? ? top: 100px;

? ? ? ? ? ? left: 30%

? ? ? ? }

? ? ? ? .box2 {

? ? ? ? ? ? width: 420px;

? ? ? ? ? ? height: 50px;

? ? ? ? ? ? border: 1px solid;

? ? ? ? ? ? background: #cccccc;

? ? ? ? ? ? float: left

? ? ? ? }

? ? ? ? .box3 {

? ? ? ? ? ? width: 0;

? ? ? ? ? ? height: 50px;

? ? ? ? ? ? background: pink;

? ? ? ? }

? ? ? ? span {

? ? ? ? ? ? display: block;

? ? ? ? ? ? width: 50px;

? ? ? ? ? ? height: 50px;

? ? ? ? ? ? font-size: 40px;

? ? ? ? ? ? float: left

? ? ? ? }

? ? </style>

</head>

<body>

? ? <div class="box1">

? ? ? ? <div class="box2">

? ? ? ? ? ? <div class="box3"></div>

? ? ? ? </div>

? ? ? ? <span></span>

? ? </div>

</body>

</html>

<script>

? ? var box1 = document.querySelector('.box1')

? ? var box2 = document.querySelector('.box2')

? ? var box3 = document.querySelector('.box3')

? ? var span=document.querySelector('span')

? ? box2.onmousedown = function (e) {

? ? ? ? e = e || widon.event;

? ? ? ? box2.onmousemove = function (e) {

? ? ? ? ? ? var x = e.offsetX

? ? ? ? ? ? box3.style.width = x + 'px'

? ? ? ? span.innerText=Math.floor(x/box2.clientWidth*100)+'%'

? ? ? ? }

? ? }

? ? box2.onmouseup = function () {

? ? ? ? box2.onmousemove = ''

? ? }


</script>

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容