hover時に画像を拡大するボタンをつくる
2023.12.29 09:00
2023.12.29 23:15

画像ボタンで、ホバーしたときに画像が拡大するギミックをつけてみました。
完成はこんな感じになります。
https://tech.exeweb.net/sample/3714/
では実際のコードです。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="style.css" type="text/css" media="all" />
</head>
<body>
<div class="container">
<div class="box">
<a href="https://google.co.jp" class="inner">
<span class="text">ボタン</span>
<img src="photo.jpg">
</a>
</div>
</div>
</body>
</html>
html, body {
margin: 0;
padding: 0;
}
.container {
position: relative;
height: 200px;
width: 300px;
}
.box {
position: absolute;
background-color: #fff;
border-radius: 5px;
display: flex;
justify-content: center;
align-items: center;
overflow: hidden;
cursor: pointer;
width: 100%;
height: 100%;
z-index: 1;
}
.box img {
height: 110%;
width: 110%;
}
.inner {
transition: 1s all;
padding: 0;
margin: 0;
}
.inner:hover{
opacity: 1;
transform: scale(1.2,1.2);
}
.text {
background-color: #fff;
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
display: flex;
justify-content: center;
align-items: center;
z-index: 2;
height: 100px;
width: 50%;
opacity: .8;
border-radius: 3px;
}
実際にやってみると意外と複雑で、ハマりやすいですね。
今回は以上です!