原生js實現(xiàn)寬度計數(shù)器
本文實例為大家分享了js實現(xiàn)寬度計數(shù)器的具體代碼,供大家參考,具體內(nèi)容如下
一.用原生js實現(xiàn)寬度計數(shù)器
1.源碼
<!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>計數(shù)器-原生js</title>
? ? <style>
? ? ? ? #box{
? ? ? ? ? ? width: 200px;
? ? ? ? ? ? height: 200px;
? ? ? ? ? ? border: 1px solid white;
? ? ? ? ? ? background-color: #878080;
? ? ? ? }
? ? ? ? /* 并集選擇器 */
? ? ? ? button,span{
? ? ? ? ? ? margin: 10px 7px;
? ? ? ? }
? ? </style>
</head>
<body>
? ? <div id="box"></div>
? ? <button id="minus" onclick="minusWidth()">-</button>
? ? <button id="add" onclick="addWidth()">+</button>
? ? <button onclick="reset()">還原</button>
? ? <span id="result">200px</span>
</body>
</html>
<script>
? ? //修改數(shù)值
? ? var count=200;
? ? document.getElementById('add').addEventListener('click',()=>{
? ? ? ? if(count>500){
? ? ? ? ? ? alert("寬度超出限制");
? ? ? ? }
? ? ? ? else{
? ? ? ? ? ? count+=10;
? ? ? ? }
? ? ? ? document.getElementById('result').innerText = count+'px'; ?
? ? });
? ? document.getElementById('minus').addEventListener('click',()=>{
? ? ? ? if(count<10){
? ? ? ? ? ?alert("寬度超出限制");
? ? ? ? }
? ? ? ? else{
? ? ? ? ? ?count-=10;
? ? ? ? }
? ? ? ? document.getElementById('result').innerText = count+'px'; ?
? ? });
? ? //修改盒子寬度
? ? var boxWidth=200;
? ? var box=document.getElementById('box');
? ? function addWidth(){
? ? ? ?if(boxWidth>500){
? ? ? ? ? alert("圖片寬度無法增加");
? ? ? ?}?
? ? ? ?else{
? ? ? ? ? boxWidth+=10;?
? ? ? ?}
? ? ? ?box.style.width=boxWidth+"px";
? ? };
? ? function minusWidth(){?
? ? ? ?if(boxWidth<10){
? ? ? ? ? alert("圖片寬度無法減少");
? ? ? ?}
? ? ? ?else{
? ? ? ? ? boxWidth-=10;
? ? ? ?}
? ? ? ?box.style.width=boxWidth+'px';
? ? };
? ? function reset(){
? ? ? ?document.getElementById('box').style='200px';
? ? ? ?document.getElementById('result').innerHTML='200px';
? ? ? ?count=200;
? ? ? ?boxWidth=200;
? ? };
</script>2.效果圖

二.總結(jié)
addEventListener:用于監(jiān)聽事件并進行處理的函數(shù)。
innerText:用于獲取文本內(nèi)容的屬性。(不包含html標簽)
innerHTML:用于獲取html標簽內(nèi)容的屬性。(識別所有html標簽)
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript本地數(shù)據(jù)存儲sessionStorage與localStorage使用詳解
這篇文章主要介紹了JavaScript本地數(shù)據(jù)存儲sessionStorage與localStorage使用,localStorage:永久存儲在本地,適合保存在本地的數(shù)據(jù)。sessionStorage:會話級的存儲,敏感帳號一次登陸2022-10-10
layui 點擊重置按鈕, select 并沒有被重置的解決方法
今天小編就為大家分享一篇layui 點擊重置按鈕, select 并沒有被重置的解決方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09
JavaScript 產(chǎn)生不重復(fù)的隨機數(shù)三種實現(xiàn)思路
在 JavaScript 中,一般產(chǎn)生的隨機數(shù)會重復(fù),但是有時我們需要不重復(fù)的隨機數(shù),如何實現(xiàn)?本文給于解決方法,需要的朋友可以參考下2012-12-12
找出字符串中出現(xiàn)次數(shù)最多的字母和出現(xiàn)次數(shù)精簡版
找出字符串中出現(xiàn)次數(shù)最多的字母和出現(xiàn)次數(shù)精簡版,有需求的朋友可以參考下2012-11-11
Javacript實現(xiàn)顏色梯度變化和漸變的效果代碼
用js對導(dǎo)航欄的顏色做了梯度的變化處理,通過處理..獲取兩種顏色在變化時的各種顏色字符串,并且字符串的個數(shù),即獲取的頻率可以調(diào)節(jié)2013-05-05

