?
平川ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書未來市場(chǎng)廣闊!成為創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18982081108(備注:SSL證書合作)期待與您的合作!
<!DOCTYPE html>
<html>
?
<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>js連連看</title>
????<style>
????????* {
????????????margin: 0;
????????????padding: 0;
????????}
?
????????html,
????????body {
????????????width: 100%;
????????????height: 100%;
????????????background: #222;
????????????overflow: hidden;
????????}
?
????????.wrapper {
????????????background-size: 100% 100%;
????????????margin: 10px auto;
????????????position: relative;
????????????/* border: 1px solid #f40; ?*/
????????}
?
????????.square {
????????????cursor: pointer;
????????????position: absolute;
????????????width: 80px;
????????????height: 80px;
????????????background-size: cover;
????????}
????</style>
</head>
?
<body>
????<div></div>
????<script>
????????var wrap = document.getElementsByClassName('wrapper')[0];
????????var rows = 7; ??// 創(chuàng)建連連看行數(shù)
????????var cols = 12; ?// 創(chuàng)建連連看列數(shù)
????????var type = 24 ??//選擇多少種圖片,0-24都可以 ?看自己心情 數(shù)字大種類多 ?數(shù)字小種類少難度更簡(jiǎn)單
????????var squareSet = []; ???// 生成小方塊的數(shù)組
????????var chooseOne = null; //
????????var chooseTwo = null; //
????????var Toward = { node: null, up: { row: -1, col: 0 }, right: { row: 0, col: 1 }, down: { row: 1, col: 0 }, left: { row: 0, col: -1 } }
????????window.onload = function () {
????????????init(); //初始化
????????}
?
????????function init() {
????????????if (rows * cols % 2 != 0) { //判斷小方塊總數(shù)是否為奇數(shù),奇數(shù)就不執(zhí)行
????????????????alert('展示數(shù)量不能為奇數(shù)') ?// ?彈出提示,阻塞js加載
????????????}
????????????initSquareSet();
????????}
????????function initSquareSet() {
????????????// 方塊默認(rèn)長(zhǎng)寬都是80px
????????????wrap.style.height = rows * 80 + 'px'; ??// 外面盒子的總高度
????????????wrap.style.width = cols * 80 + 'px'; ?// 外面盒子的總寬度
????????????var oDiv = document.createElement('div')
????????????var tmp = createRandomNum(); ?//生成隨機(jī)數(shù)數(shù)組 ??我的圖片名稱是0.jpg~24.jpg函數(shù)生成0~24隨機(jī)數(shù)就可以通過字符串拼接動(dòng)態(tài)的選擇圖片
?
????????????squareSet = new Array(rows + 2); ?// 生成小方塊的數(shù)組 ??既有行又有列 ?我們就要利用for循環(huán)生成二維數(shù)組57~60
????????????for (var i = 0; i < squareSet.length; i++) {
????????????????squareSet[i] = new Array(cols + 2);
????????????}
?
????????????for (var i = 1; i <= rows; i++) { ?// 生成行數(shù) ??
????????????????for (var j = 1; j <= cols; j++) { // 生成列數(shù) 同理
????????????????????var temp = createSquare(tmp.pop(), i, j); // 參數(shù)每次取隨機(jī)數(shù)數(shù)組的最后一位 ?i小方塊在整體中行的位置j是列的位置 ??temp接收這個(gè)返回的DOM元素
????????????????????squareSet[i][j] = temp;
????????????????????wrap.append(temp);
????????????????????temp.onclick = function () {
????????????????????????if (chooseOne == null || chooseOne.num != this.num) { ??// 判斷是第一次點(diǎn)擊還是第二次77~81沒有值或者說沒有屬性的都是第一次點(diǎn)擊
????????????????????????????chooseOne = this;
????????????????????????} else {
????????????????????????????chooseTwo = this;
????????????????????????????if (chooseOne != chooseTwo && chooseOne.num == chooseTwo.num ) { //判斷第一次和第二次點(diǎn)擊不是同一個(gè) 并且num值相等 ?以及是否在路徑上可以消除
????????????????????????????????clearSquare(chooseOne.row, chooseOne.col);
????????????????????????????????clearSquare(chooseTwo.row, chooseTwo.col);
????????????????????????????}
????????????????????????????chooseOne = null;
????????????????????????????chooseTwo = null;
????????????????????????}
????????????????????????render(); // 點(diǎn)擊方塊變換樣式
????????????????????}
????????????????}
????????????}
?
????????}
????????function createRandomNum() {
????????????var tmp = [] // 存放生成圖片是 ?字符串拼接的數(shù)字
?
????????????// rows * cols 可以算出需要多少張圖片 然后除以2因?yàn)槊繌垐D片都是成對(duì)出現(xiàn)的 ?即7*12=84張圖片 ?84/2=41算出有42對(duì)
????????????for (var i = 0; i < rows * cols / 2; i++) {
????????????????var num = Math.floor(Math.random() * type) ?// 生成0~24的隨機(jī)數(shù)
????????????????tmp.push(num);
????????????????tmp.push(num); // 循環(huán)了42次 ?所以push兩遍 相當(dāng)如每次push了相同的一對(duì)數(shù),42次 正好84張圖片
????????????}
????????????// console.log(tmp) ?// 看看生成的數(shù)組 ?可以看到成對(duì)的隨機(jī)數(shù)字?jǐn)?shù)組
????????????tmp.sort(function () {
????????????????return Math.random() - 0.5 ?//可以打亂數(shù)組
????????????})
????????????// console.log(tmp) ?// 看看生成的數(shù)組 ?可以看到已經(jīng)不成對(duì)的隨機(jī)數(shù)字?jǐn)?shù)組
????????????function(){ //技術(shù)指標(biāo) http://www.kaifx.cn/mt4/kaifx/1734.html
????????????return tmp ??????????// 將數(shù)組返去
?
????????}
????????function createSquare(num, row, col) {
?
????????????var temp = document.createElement('div');
????????????temp.classList.add('square');
????????????temp.style.backgroundImage = "url('./src/img/連連看/" + num + ".jpg')";
????????????temp.style.top = row * 80 + 'px'; // 生成方塊的位置96,97
????????????temp.style.left = col * 80 + 'px';
????????????temp.num = num; //設(shè)置小方塊的隨機(jī)數(shù)屬性 到時(shí)候可以用來判斷屬性是否一樣來判斷是否消除小方塊
?
????????????return temp; ??//返回了一個(gè)帶著屬性的DOM元素
????????}
?
????????function render() {
????????????for (var i = 0; i < squareSet.length; i++) { ?//做一個(gè)樣式的切換
????????????????for (var j = 0; j < squareSet[i].length; j++) {
????????????????????if (squareSet[i][j] && squareSet[i][j] == chooseOne) {
????????????????????????squareSet[i][j].style.opacity = '0.5';
????????????????????} else if (squareSet[i][j]) {
????????????????????????squareSet[i][j].style.opacity = '1';
????????????????????}
????????????????}
????????????}
?
????????}
????????function clearSquare(x, y) {
????????????wrap.removeChild(squareSet[x][y]); // 刪除方塊
????????????squareSet[x][y] = null;
????????}
?
????</script>
</body>
?
</html>
分享名稱:js清除圖片
當(dāng)前地址:http://chinadenli.net/article0/jggiio.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、標(biāo)簽優(yōu)化、移動(dòng)網(wǎng)站建設(shè)、網(wǎng)站營銷、網(wǎng)站收錄、App設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)