marginの位置が画面の大きさによって変わるので
画面の大きさが変わってもストーリーアイコンの真ん中にくるようにしたいです。
vwや%を使ってもmarginがずれます。
画面いっぱいの場合
画面の3分の1
html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <link rel="stylesheet" href="css/destyle.css"> <link rel="stylesheet" type="text/css" href="https://coco-factory.jp/ugokuweb/wp-content/themes/ugokuweb/data/reset.css"> <link rel="stylesheet" type="text/css" href="css/style.css"> <title>Document</title> </head> <body> <div class="container"> <nav class="navigation"> <ul id="page-link"> <li class="list active"> <a href="#story"> <span class="icon"><ion-icon name="chatbox-ellipses-outline"></ion-icon></span> <span class="text">ストーリー</span> </a> </li> <li class="list"> <a href="#chara"> <span class="icon"><ion-icon name="person-outline"></ion-icon></span> <span class="text">キャラクター&声優</span> </a> </li> <li class="list"> <a href="#midokoro"> <span class="icon"><ion-icon name="glasses-outline"></ion-icon></span> <span class="text">見どころ</span> </a> </li> <div class="indicator"></div> </ul> </nav> </div> </body> <script src='js/bdd.js'></script> <script type="module" src="https://unpkg.com/ionicons@5.5.2/dist/ionicons/ionicons.esm.js"></script> <script nomodule src="https://unpkg.com/ionicons@5.5.2/dist/ionicons/ionicons.js"></script> <script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script> </html>
style.css
:root{ --clr: #fff; } .navigation{ position: relative; width: 100%; height: 70px; background:turquoise; display: flex; justify-content: center; align-items: center; border-radius: 10px; margin-top: 15px; } .navigation ul{ position: relative; display: flex; width: 100%; } .navigation ul li{ position: relative; list-style: none; width: 33vw; height: 70px; z-index: 1; } .navigation ul li a{ position: relative; display: flex; justify-content: center; align-items: center; flex-direction: column; width: 100%; text-align: center; font-weight: 500; } .navigation ul li a .icon{ position: relative; display: block; line-height: 75px; font-size: 1.5em; text-align: center; transition: 0.5s; color: var(--clr); } .navigation ul li.active a .icon{ transform: translateY(-32px); } .navigation ul li a .text{ position: absolute; color: var(--clr); font-weight: 400; font-size: 0.75em; letter-spacing: 0.05em; transition: 0.5s; opacity: 0; transform: translateY(20px); } .navigation ul li.active a .text{ opacity: 1; transform: translateY(10px); } .indicator{ position: absolute; top: -52%; margin-left: 14.2vw; width: 70px; height: 70px; background: #29fd53; border-radius: 50%; border: 6px solid var(--clr); transition: 0.5s; } .indicator::before{ content: ''; position: absolute; top: 50%; left:-22px; width: 20px; height: 20px; background: transparent; border-top-right-radius: 20px; box-shadow: 1px -10px 0 0 var(--clr); } .indicator::after{ content: ''; position: absolute; top: 50%; right:-22px; width: 20px; height: 20px; background: transparent; border-top-left-radius: 20px; box-shadow: -1px -10px 0 0 var(--clr); } .navigation ul li:nth-child(1).active ~ .indicator{ transform: translateX(calc(33vw*0)); } .navigation ul li:nth-child(2).active ~ .indicator{ transform: translateX(calc(33vw*1)); } .navigation ul li:nth-child(3).active ~ .indicator{ transform: translateX(calc(33vw*2)); }
bdd.js
const list = document.querySelectorAll('.list'); function activeLink(){ list.forEach((item) => item.classList.remove('active')); this.classList.add('active'); } list.forEach((item) => item.addEventListener('mouseover',activeLink));
回答1件
あなたの回答
tips
プレビュー