亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用z-index模擬通過四連接板掉落的芯片以使其具有3d效果?

如何使用z-index模擬通過四連接板掉落的芯片以使其具有3d效果?

米琪卡哇伊 2021-05-14 14:09:29
我正在進行四局連通游戲,現在我可以將芯片放入適當的插槽中,也可以從紅籌變為黃籌。但是,當您放下芯片時,它不會進入電路板。它位于板的外部。我希望被丟棄的芯片落在每個插槽內的深藍色圓圈上,并落在插槽本身之下。因此,它看起來很逼真的3D效果。我以為我可以使用z-index做到這一點,但是我有2個問題。1st當我將div槽設置為3的z-index時,即使下降的籌碼的z-index為2;芯片仍然掉在插槽上嗎?第二,即使確實可行,由于div的z索引較高,每個槽口中的深藍色圓圈現在也將被隱藏,但它們必須保持相同才能使它們都可見。但是,如果它們相同,那么芯片不能落入電路板中嗎?關于如何產生這種效果的任何想法?//grab all slot positions on the boardconst slots = document.querySelectorAll('.board div');let player = 'p1';let board = [     0, 1, 2, 3, 4, 5, 6,    7, 8, 9, 10, 11, 12, 13,    14, 15, 16, 17, 18, 19, 20,    21, 22, 23, 24, 25, 26, 27,    28, 29, 30, 31, 32, 33, 34,    35, 36, 37, 38, 39, 40, 41,]//assign a class to each slot to represent its positionfor(let i = 0; i < slots.length; i++) {    //add class to each div    slots[i].classList.add('c' + i);    //add the slot to each div    let slot = document.createElement('span');    slots[i].appendChild(slot);    //add the function with the game logic to each slot    slots[i].addEventListener('click', runGame); }function runGame() {    //figure out which column the selected slot sits in    const slotColumn = (Number(this.className.slice(1, 3)) % 7);    //create an array to store all the slots that share the above column    const columnArray = [];    //grab all the slots that sit in that column    for(let i = 0; i < board.length; i++) {        if(board[i] % 7 === slotColumn) columnArray.push(board[i]);    }    //drop chip in the chosen column    dropChip(columnArray);    function dropChip(column) {        //select bottom most slot that's available in the column        for(let i = column.length - 1; i >= 0; i--) {            if(column[i] !== 'p1' || column[i] !== 'p2') {                board[column[i]] = player;                slots[column[i]].classList.add(player);                switchPlayer(player);                break;            }           }        function switchPlayer(currentPlayer) {            if(currentPlayer === 'p1') player = 'p2';            else if(currentPlayer ==='p2') player = 'p1';        }    }}
查看完整描述

1 回答

?
泛舟湖上清波郎朗

TA貢獻1818條經驗 獲得超3個贊

這是您的代碼的修改后的版本。首先,我將芯片元素更改為僅考慮一個偽元素而不是2,并且使用CSS變量來輕松更改顏色。


然后,對于電路板,我使用兩個元素創建了每個單元,以便能夠具有3D效果。您將看到偽元素,我在其中應用了徑向漸變以創建孔,并且該層將位于芯片將落在后面的頂部:


//grab all slot positions on the board

const slots = document.querySelectorAll('.board div');

let player = 'p1';

let board = [

  0, 1, 2, 3, 4, 5, 6,

  7, 8, 9, 10, 11, 12, 13,

  14, 15, 16, 17, 18, 19, 20,

  21, 22, 23, 24, 25, 26, 27,

  28, 29, 30, 31, 32, 33, 34,

  35, 36, 37, 38, 39, 40, 41,

]


//assign a class to each slot to represent its position

for (let i = 0; i < slots.length; i++) {

  //add class to each div

  slots[i].classList.add('c' + i);

  //add the slot to each div

  let slot = document.createElement('span');

  slots[i].appendChild(slot);

  //add the function with the game logic to each slot

  slots[i].addEventListener('click', runGame);

}


function runGame() {

  //figure out which column the selected slot sits in

  const slotColumn = (Number(this.className.slice(1, 3)) % 7);

  //create an array to store all the slots that share the above column

  const columnArray = [];


  //grab all the slots that sit in that column

  for (let i = 0; i < board.length; i++) {

    if (board[i] % 7 === slotColumn) columnArray.push(board[i]);

  }


  //drop chip in the chosen column

  dropChip(columnArray);


  function dropChip(column) {

    //select bottom most slot that's available in the column

    for (let i = column.length - 1; i >= 0; i--) {

      if (column[i] !== 'p1' || column[i] !== 'p2') {

        board[column[i]] = player;

        slots[column[i]].classList.add(player);

        switchPlayer(player);

        break;

      }

    }


    function switchPlayer(currentPlayer) {

      if (currentPlayer === 'p1') player = 'p2';

      else if (currentPlayer === 'p2') player = 'p1';

    }

  }

}

/** {

    outline: 1px solid red;

}*/


*,

*:before,

*:after {

  box-sizing: inherit;

}


html {

  box-sizing: border-box;

}


html,

body {

  margin: 0;

  padding: 0;

  background-color: #e5e6e8;

}


body {

  display: flex;

  justify-content: center;

  min-height: 100vh;

}


.board-wrapper {

  padding-top: 100px;

  display: flex;

  justify-content: center;

  margin: auto auto 0 auto; /*ask why this is needed*/

  position: relative;

  overflow: hidden;

}


.board {

  display: flex;

  flex-wrap: wrap;

  max-width: 706px;

  background-color: #00c;

  padding: 3px;

}


.board div {

  width: 100px;

  height: 100px;

  position: relative;

}


.board div span {

  width: 80px;

  height: 80px;

  border-radius: 50%;

  background-color: #00c;

  position: absolute;

  left: 0;

  top: 0;

  right: 0;

  bottom: 0;

  margin: auto;

  box-shadow: inset 0px 0px 13px #0606aa;

}


.board div span:before {

  content: "";

  position: absolute;

  top: -10px;

  left: -10px;

  right: -10px;

  bottom: -10px;

  background: radial-gradient(circle, transparent 40px, blue 0);

  border: 3px solid #00c;

  z-index: 3;

}


.board .chip {

  display: block;

  position: absolute;

  background-color: transparent;

  top: 0;

  left: 0;

  right: 0;

  height: 100px;

}


.board .chip:after {

  content: "";

  width: 80px;

  height: 80px;

  border-radius: 50%;

  border: 15px solid red;

  background-color: red;

  box-shadow: inset 0px 0px 20px #cc0000;

  position: absolute;

  left: 3px;

  top: 0;

  opacity: 0;

  transition: all .5s ease;

}


.board div:nth-of-type(7n+1):hover~.chip:after {

  transform: translateX(10px);

  opacity: 1;

}


.board div:nth-of-type(7n+1):hover~.chip:before {

  transform: translateX(10px);

  opacity: 1;

}


.board div:nth-of-type(7n+2):hover~.chip:after {

  transform: translateX(110px);

  opacity: 1;

}


.board div:nth-of-type(7n+2):hover~.chip:before {

  transform: translateX(110px);

  opacity: 1;

}


.board div:nth-of-type(7n+3):hover~.chip:after {

  transform: translateX(210px);

  opacity: 1;

}


.board div:nth-of-type(7n+3):hover~.chip:before {

  transform: translateX(210px);

  opacity: 1;

}


.board div:nth-of-type(7n+4):hover~.chip:after {

  transform: translateX(310px);

  opacity: 1;

}


.board div:nth-of-type(7n+4):hover~.chip:before {

  transform: translateX(310px);

  opacity: 1;

}


.board div:nth-of-type(7n+5):hover~.chip:after {

  transform: translateX(410px);

  opacity: 1;

}


.board div:nth-of-type(7n+5):hover~.chip:before {

  transform: translateX(410px);

  opacity: 1;

}


.board div:nth-of-type(7n+6):hover~.chip:after {

  transform: translateX(510px);

  opacity: 1;

}


.board div:nth-of-type(7n+6):hover~.chip:before {

  transform: translateX(510px);

  opacity: 1;

}


.board div:nth-of-type(7n+7):hover~.chip:after {

  transform: translateX(610px);

  opacity: 1;

}


.board div:nth-of-type(7n+7):hover~.chip:before {

  transform: translateX(610px);

  opacity: 1;

}


.p1:after,

.p2:after {

  content: "";

  display: inline-block;

  width: 80px;

  height: 80px;

  border-radius: 50%;

  border: 15px solid var(--c, red);

  background-color: var(--c, red);

  box-shadow: inset 0px 0px 20px var(--s, #cc0000);

  position: absolute;

  left: 0;

  top: 0;

  right: 0;

  bottom: 0;

  margin: auto;

  z-index: 1;

  animation-name: drop;

  animation-fill-mode: forwards;

  animation-duration: .5s;

  animation-timing-function: ease-in;

}


.p2 {

  --c: yellow;

  --s: #ced639;

}


@keyframes drop {

  from {

    top: -1500px;

  }

  to {

    top: 0;

  }

}

<div class="board-wrapper">

  <div class="board">

    <div></div>

    <div></div>

    <div></div>

    <div></div>

    <div></div>

    <div></div>

    <div></div>

    <div></div>

    <div></div>

    <div></div>

    <div></div>

    <div></div>

    <div></div>

    <div></div>

    <div></div>

    <div></div>

    <div></div>

    <div></div>

    <div></div>

    <div></div>

    <div></div>

    <div></div>

    <div></div>

    <div></div>

    <div></div>

    <div></div>

    <div></div>

    <div></div>

    <div></div>

    <div></div>

    <div></div>

    <div></div>

    <div></div>

    <div></div>

    <div></div>

    <div></div>

    <div></div>

    <div></div>

    <div></div>

    <div></div>

    <div></div>

    <div></div>

    <span class="chip"></span>

  </div>

</div>


查看完整回答
反對 回復 2021-05-27
  • 1 回答
  • 0 關注
  • 152 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號