单击按钮时如何使图像缩放

所以我正在做一个学校项目,因此,我必须制作一个网站,只能使用 html、css 和 javascript。我目前正在制作一个登陆页面,并有一个按钮可以将用户带到另一个页面。我想让它在单击按钮时放大背景图像,然后将用户带到另一个页面。到目前为止,这是我的代码:

CSS 然后是 HTML

* {

  margin: 0;

  padding: 0;

}


body {

  margin: 0;

  font-family: Brush Script MT;

  font-size: 17px;

  color: #926239;

  line-height: 1.6;

}


#showcase {

  background-image: url('https://image.freepik.com/free-vector/cartoon-cave-with-stalactites_29190-1074.jpg');

  background-size: cover;

  background-position: center;

  height: 100vh;

  display: flex;

  flex-direction: column;

  justify-content: center;

  align-items: center;

  text-align: center;

  padding: 0 20px;

}


#showcase h1 {

  font-size: 10px;

  line-height: 1.2;

}


#showcase p {

  font-size: 20px;

}


#showcase .button {

  font-size: 25px;

  text-decoration: none;

  color: #10B589;

  border: #10B589 1px solid;

  padding: 10px 20px;

  border-radius: 10px;

  margin-top: 20px;

}


#showcase .button:hover {

  background: #10B589;

  color: #fff;

  animation: wiggle 1s;

  box-sizing: border-box;

}


#section-a {

  padding: 20px;

  background: #10B589;

  color: #fff;

  text-align: center;

}


#section-b {

  padding: 20px;

  background: #10B589;

  text-align: center;

}


#section-c {

  display: flex;

}


#section-c div {

  padding: 20px;

}


#section-c .box-1,

#section-c .box-3 {

  background: #10B589;

  color: #fff;

}


#section-c .box-2 {

  background: #f9f9f9;

}


@keyframes wiggle {

  12% { transform: scale(0.4,  0.65); }

  13% { transform: scale(0.43, 0.67); }

  14% { transform: scale(0.5,  0.76); }

  25% { transform: scale(0.8,  1.3);  }

  37% { transform: scale(0.9,  0.95); }

  50% { transform: scale(1.1,  0.8);  }

  62% { transform: scale(0.9,  1);    }

  75% { transform: scale(0.7,  1.2);  }

  87% { transform: scale(0.8,  1.1);  }

}

<html>

<head>

  <header id="showcase">

    <a href="insertname.html" class="button">Enter The Cave</a>

  </header>

  <body>

  </body>

</head>

<link href="ISTwebsite.css" rel="stylesheet" type="text/css">

</html>

我在互联网上尝试了很多其他问题,有人可以帮助我吗?



慕的地6264312
浏览 91回答 5
5回答

胡说叔叔

这是一个动画背景缩放示例,平滑地失去了可见性。添加了动态类background_size。请注意 js 代码中的行:您需要插入链接document.location.href = "your_link";而不是。your_linklet button_a = document.querySelector('.button');let background_picture = document.querySelector('#showcase');button_a.onclick = function(){&nbsp; background_picture.classList.add('background_size');&nbsp; button_a.classList.add('smooth_button');&nbsp; document.location.href = "your_link";};* {&nbsp; margin: 0;&nbsp; padding: 0;}body {&nbsp; margin: 0;&nbsp; font-family: Brush Script MT;&nbsp; font-size: 17px;&nbsp; color: #926239;&nbsp; line-height: 1.6;}#showcase {&nbsp; background-image: url('https://image.freepik.com/free-vector/cartoon-cave-with-stalactites_29190-1074.jpg');&nbsp; background-size: cover;&nbsp; background-position: center;&nbsp; height: 100vh;&nbsp; display: flex;&nbsp; flex-direction: column;&nbsp; justify-content: center;&nbsp; align-items: center;&nbsp; text-align: center;&nbsp; padding: 0 20px;&nbsp; transition: 0.5s;&nbsp; opacity: 1;}.background_size {&nbsp; transform: scale(1.5);&nbsp; opacity: 0!important;}.smooth_button {&nbsp; opacity: 0!important;}#showcase h1 {&nbsp; font-size: 10px;&nbsp; line-height: 1.2;}#showcase p {&nbsp; font-size: 20px;}#showcase .button {&nbsp; font-size: 25px;&nbsp; text-decoration: none;&nbsp; color: #10B589;&nbsp; border: #10B589 1px solid;&nbsp; padding: 10px 20px;&nbsp; border-radius: 10px;&nbsp; margin-top: 20px;&nbsp; transition: 0.1s;&nbsp; opacity: 1;}#showcase .button:hover {&nbsp; background: #10B589;&nbsp; color: #fff;&nbsp; animation: wiggle 1s;&nbsp; box-sizing: border-box;}#section-a {&nbsp; padding: 20px;&nbsp; background: #10B589;&nbsp; color: #fff;&nbsp; text-align: center;}#section-b {&nbsp; padding: 20px;&nbsp; background: #10B589;&nbsp; text-align: center;}#section-c {&nbsp; display: flex;}#section-c div {&nbsp; padding: 20px;}#section-c .box-1,#section-c .box-3 {&nbsp; background: #10B589;&nbsp; color: #fff;}#section-c .box-2 {&nbsp; background: #f9f9f9;}@keyframes wiggle {&nbsp; 12% { transform: scale(0.4,&nbsp; 0.65); }&nbsp; 13% { transform: scale(0.43, 0.67); }&nbsp; 14% { transform: scale(0.5,&nbsp; 0.76); }&nbsp; 25% { transform: scale(0.8,&nbsp; 1.3);&nbsp; }&nbsp; 37% { transform: scale(0.9,&nbsp; 0.95); }&nbsp; 50% { transform: scale(1.1,&nbsp; 0.8);&nbsp; }&nbsp; 62% { transform: scale(0.9,&nbsp; 1);&nbsp; &nbsp; }&nbsp; 75% { transform: scale(0.7,&nbsp; 1.2);&nbsp; }&nbsp; 87% { transform: scale(0.8,&nbsp; 1.1);&nbsp; }}<html><head>&nbsp; <header id="showcase">&nbsp; &nbsp; <a class="button">Enter The Cave</a>&nbsp; </header>&nbsp; <body>&nbsp; </body></head><link href="ISTwebsite.css" rel="stylesheet" type="text/css"></html>

MMTTMM

您可以在几秒钟后简单地分配a's href,点击事件和背景缩放就像我的代码中的以下内容一样发生btn.onclick = () => {&nbsp; showcase.style.transition = "1.5s";&nbsp; showcase.style.transform = "scale(1.3)";&nbsp; setTimeout(() => {&nbsp; &nbsp; window.location.assign("insertname.html");&nbsp; }, 1000);}* {&nbsp; margin: 0;&nbsp; padding: 0;}body {&nbsp; margin: 0;&nbsp; font-family: Brush Script MT;&nbsp; font-size: 17px;&nbsp; color: #926239;&nbsp; line-height: 1.6;}#showcase {&nbsp; background-image: url('https://image.freepik.com/free-vector/cartoon-cave-with-stalactites_29190-1074.jpg');&nbsp; background-size: cover;&nbsp; background-position: center;&nbsp; height: 100vh;&nbsp; display: flex;&nbsp; flex-direction: column;&nbsp; justify-content: center;&nbsp; align-items: center;&nbsp; text-align: center;&nbsp; padding: 0 20px;}#showcase h1 {&nbsp; font-size: 10px;&nbsp; line-height: 1.2;}#showcase p {&nbsp; font-size: 20px;}#showcase .button {&nbsp; font-size: 25px;&nbsp; text-decoration: none;&nbsp; color: #10B589;&nbsp; border: #10B589 1px solid;&nbsp; padding: 10px 20px;&nbsp; border-radius: 10px;&nbsp; margin-top: 20px;}#showcase .button:hover {&nbsp; background: #10B589;&nbsp; color: #fff;&nbsp; animation: wiggle 1s;&nbsp; box-sizing: border-box;}#section-a {&nbsp; padding: 20px;&nbsp; background: #10B589;&nbsp; color: #fff;&nbsp; text-align: center;}#section-b {&nbsp; padding: 20px;&nbsp; background: #10B589;&nbsp; text-align: center;}#section-c {&nbsp; display: flex;}#section-c div {&nbsp; padding: 20px;}#section-c .box-1,#section-c .box-3 {&nbsp; background: #10B589;&nbsp; color: #fff;}#section-c .box-2 {&nbsp; background: #f9f9f9;}@keyframes wiggle {&nbsp; 12% {&nbsp; &nbsp; transform: scale(0.4, 0.65);&nbsp; }&nbsp; 13% {&nbsp; &nbsp; transform: scale(0.43, 0.67);&nbsp; }&nbsp; 14% {&nbsp; &nbsp; transform: scale(0.5, 0.76);&nbsp; }&nbsp; 25% {&nbsp; &nbsp; transform: scale(0.8, 1.3);&nbsp; }&nbsp; 37% {&nbsp; &nbsp; transform: scale(0.9, 0.95);&nbsp; }&nbsp; 50% {&nbsp; &nbsp; transform: scale(1.1, 0.8);&nbsp; }&nbsp; 62% {&nbsp; &nbsp; transform: scale(0.9, 1);&nbsp; }&nbsp; 75% {&nbsp; &nbsp; transform: scale(0.7, 1.2);&nbsp; }&nbsp; 87% {&nbsp; &nbsp; transform: scale(0.8, 1.1);&nbsp; }}<header id="showcase">&nbsp; <a class="button" id="btn">Enter The Cave</a></header>

白板的微信

首先,我修复了你的动画 CSS ...@keyframes wiggle {&nbsp; 0% { transform: scale(1.2,&nbsp; 1.2); }&nbsp; 15% { transform: scale(1.3, 1.3); }&nbsp; 30% { transform: scale(1.4, 1.4); }&nbsp; 45% { transform: scale(1.5, 1.5); }&nbsp; 60% { transform: scale(1.6, 1.6); }&nbsp; 75% { transform: scale(1.7, 1.7); }&nbsp; 90% { transform: scale(1.8, 1.8); }&nbsp; 100% { transform: scale(1.9,1.9); }}其次,我将 JS 添加到您按钮的 onclick 中......<a href="insertname.html" class="button" onclick="event.preventDefault(); showcase.style.animation='wiggle 3s linear 0s infinite normal none'; setTimeout(function(){document.location=this.href;},2000);">Enter The Cave</a>在 onclick 里面你会发现...事件.preventDefault();&nbsp;// 停止锚点导航showcase.style.animation='wiggle 3s linear 0s infinite normal none';&nbsp;// 将动画 CSS 添加到图像setTimeout(函数(){document.location=this.href;},2000);&nbsp;// 延迟 2 秒后将页面更改为所需的 href。

Smart猫小萌

我也把我的小图书馆放在那里。看下// library above - magic under here://<![CDATA[/* js/external.js */let get, post, doc, htm, bod, nav, M, I, mobile, S, Q, hC, aC, rC, tC, shuffle, rand; // for use on other loadsaddEventListener('load', ()=>{get = (url, success, responseType = 'json', context = null)=>{&nbsp; const x = new XMLHttpRequest;&nbsp; const c = context || x;&nbsp; x.open('GET', url); x.responseType = responseType;&nbsp; x.onload = ()=>{&nbsp; &nbsp; if(success)success.call(c, x.response);&nbsp; }&nbsp; x.send();&nbsp; return x;}post = function(url, send, success, responseType ='json', context = null){&nbsp; const x = new XMLHttpRequest;&nbsp; const c = context || x;&nbsp; x.open('POST', url); x.responseType = responseType;&nbsp; x.onload = ()=>{&nbsp; &nbsp; if(success)success.call(c, x.response);&nbsp; }&nbsp; if(typeof send === 'object' && send && !(send instanceof Array)){&nbsp; &nbsp; if(send instanceof FormData){&nbsp; &nbsp; &nbsp; x.send(send);&nbsp; &nbsp; }&nbsp; &nbsp; else{&nbsp; &nbsp; &nbsp; const fd = new FormData;&nbsp; &nbsp; &nbsp; let s;&nbsp; &nbsp; &nbsp; for(let k in send){&nbsp; &nbsp; &nbsp; &nbsp; s = send[k];&nbsp; &nbsp; &nbsp; &nbsp; if(typeof s === 'object' && s)s = JSON.stringify(s);&nbsp; &nbsp; &nbsp; &nbsp; fd.append(k, s);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; x.send(fd);&nbsp; &nbsp; }&nbsp; }&nbsp; else{&nbsp; &nbsp; throw new Error('send argument must be an Object');&nbsp; }&nbsp; return x;}doc = document; htm = doc.documentElement; bod = doc.body; nav = navigator; M = tag=>doc.createElement(tag); I = id=>doc.getElementById(id);mobile = nav.userAgent.match(/Mobi/i) ? true : false;S = (selector, within)=>{&nbsp; let w = within || doc;&nbsp; return w.querySelector(selector);}Q = (selector, within)=>{&nbsp; let w = within || doc;&nbsp; return w.querySelectorAll(selector);}hC = function(node, className){&nbsp; return node.classList.contains(className);}aC = function(){&nbsp; const a = [].slice.call(arguments), n = a.shift();&nbsp; n.classList.add(...a);&nbsp; return aC;}rC = function(){&nbsp; const a = [].slice.call(arguments), n = a.shift();&nbsp; n.classList.remove(...a);&nbsp; return rC;}tC = function(){&nbsp; const a = [].slice.call(arguments), n = a.shift();&nbsp; n.classList.toggle(...a);&nbsp; return tC;}shuffle = array=>{&nbsp; let a = array.slice(), i = a.length, n, h;&nbsp; while(i){&nbsp; &nbsp; n = Math.floor(Math.random()*i--); h = a[i]; a[i] = a[n]; a[n] = h;&nbsp; }&nbsp; return a;}rand = (min, max)=>{&nbsp; let mn = min, mx = max;&nbsp; if(mx === undefined){&nbsp; &nbsp; mx = mn; mn = 0;&nbsp; }&nbsp; return mn+Math.floor(Math.random()*(mx-mn+1));}// library above - magic under hereconst stage = I('stage'), enter = I('enter');enter.onclick = function(){&nbsp; aC(this, 'dis'); aC(stage, 'cave');&nbsp; setTimeout(()=>{&nbsp; &nbsp; location = 'insertname.html';&nbsp; }, 520);}}); // end load//]]>/* css/external.css */*{&nbsp; box-sizing:border-box; color:#000; padding:0; margin:0; overflow:hidden;}body,html,.main,#stage{&nbsp; width:100vw; height:100vh;}.hid{&nbsp; display:none;}.dis{&nbsp; opacity:0; transition:opacity 0.1s;}#stage{&nbsp; display:flex; background:center / cover url('https://image.freepik.com/free-vector/cartoon-cave-with-stalactites_29190-1074.jpg'); align-items:center; justify-content:center;}#stage.cave{&nbsp; transform:scale(5); transition:transform 0.5s;}#enter{&nbsp; background:transparent; color: #10B589; font:25px 'Brush Script MT', cursive; padding:15px 25px; border:#10B589 1px solid; border-radius:10px; margin-top:20px; cursor:pointer;}#enter:hover{&nbsp; background:#10B589; color:#fff; animation:wiggle 1s;}@keyframes wiggle {&nbsp; 12% { transform: scale(0.4,&nbsp; 0.65); }&nbsp; 13% { transform: scale(0.43, 0.67); }&nbsp; 14% { transform: scale(0.5,&nbsp; 0.76); }&nbsp; 25% { transform: scale(0.8,&nbsp; 1.3); }&nbsp; 37% { transform: scale(0.9,&nbsp; 0.95); }&nbsp; 50% { transform: scale(1.1,&nbsp; 0.8); }&nbsp; 62% { transform: scale(0.9,&nbsp; 1); }&nbsp; 75% { transform: scale(0.7,&nbsp; 1.2); }&nbsp; 87% { transform: scale(0.8,&nbsp; 1.1); }}<!DOCTYPE html><html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en'>&nbsp; <head>&nbsp; &nbsp; <meta charset='UTF-8' /><meta name='viewport' content='width=device-width, height=device-height, initial-scale:1, user-scalable=no' />&nbsp; &nbsp; <title>Title Here</title>&nbsp; &nbsp; <link type='text/css' rel='stylesheet' href='css/external.css' />&nbsp; &nbsp; <script src='js/external.js'></script>&nbsp; </head><body>&nbsp; <div class='main'>&nbsp; &nbsp; <div id='stage'>&nbsp; &nbsp; &nbsp; <input id='enter' type='button' value='Enter the Cave' />&nbsp; &nbsp; </div>&nbsp; </div></body></html>

米脂

您可以尝试添加另一个子元素,并添加一个脚本以在转到另一个页面之前将其放大一点以产生缩放效果。CSS(ISTwebsite.css):* {&nbsp; margin: 0;&nbsp; padding: 0;}body {&nbsp; margin: 0;&nbsp; font-family: Brush Script MT;&nbsp; font-size: 17px;&nbsp; color: #926239;&nbsp; line-height: 1.6;}#showcase {&nbsp; height: 100vh;&nbsp; width: 100%;}.inner-showcase {&nbsp; background-image: url('https://image.freepik.com/free-vector/cartoon-cave-with-stalactites_29190-1074.jpg');&nbsp; background-size: cover;&nbsp; background-position: center;&nbsp; height: 100%;&nbsp; width: 100%;&nbsp; display: flex;&nbsp; flex-direction: column;&nbsp; justify-content: center;&nbsp; align-items: center;&nbsp; text-align: center;&nbsp; padding: 0 20px;}.inner-showcase h1 {&nbsp; font-size: 10px;&nbsp; line-height: 1.2;}.inner-showcase p {&nbsp; font-size: 20px;}.inner-showcase .button {&nbsp; font-size: 25px;&nbsp; text-decoration: none;&nbsp; color: #10B589;&nbsp; border: #10B589 1px solid;&nbsp; padding: 10px 20px;&nbsp; border-radius: 10px;&nbsp; margin-top: 20px;}.inner-showcase .button:hover {&nbsp; background: #10B589;&nbsp; color: #fff;&nbsp; animation: wiggle 1s;&nbsp; box-sizing: border-box;}#section-a {&nbsp; padding: 20px;&nbsp; background: #10B589;&nbsp; color: #fff;&nbsp; text-align: center;}#section-b {&nbsp; padding: 20px;&nbsp; background: #10B589;&nbsp; text-align: center;}#section-c {&nbsp; display: flex;}#section-c div {&nbsp; padding: 20px;}#section-c .box-1,#section-c .box-3 {&nbsp; background: #10B589;&nbsp; color: #fff;}#section-c .box-2 {&nbsp; background: #f9f9f9;}@keyframes wiggle {&nbsp; 12% {&nbsp; &nbsp; transform: scale(0.4, 0.65);&nbsp; }&nbsp; 13% {&nbsp; &nbsp; transform: scale(0.43, 0.67);&nbsp; }&nbsp; 14% {&nbsp; &nbsp; transform: scale(0.5, 0.76);&nbsp; }&nbsp; 25% {&nbsp; &nbsp; transform: scale(0.8, 1.3);&nbsp; }&nbsp; 37% {&nbsp; &nbsp; transform: scale(0.9, 0.95);&nbsp; }&nbsp; 50% {&nbsp; &nbsp; transform: scale(1.1, 0.8);&nbsp; }&nbsp; 62% {&nbsp; &nbsp; transform: scale(0.9, 1);&nbsp; }&nbsp; 75% {&nbsp; &nbsp; transform: scale(0.7, 1.2);&nbsp; }&nbsp; 87% {&nbsp; &nbsp; transform: scale(0.8, 1.1);&nbsp; }}HTML:<html>&nbsp; <head>&nbsp; &nbsp; <link href="ISTwebsite.css" rel="stylesheet" type="text/css">&nbsp; </head>&nbsp; <body>&nbsp; &nbsp; <header id="showcase">&nbsp; &nbsp; &nbsp; <div class="inner-showcase">&nbsp; &nbsp; &nbsp; &nbsp; <a href="insertname.html" class="button myLink">Enter The Cave</a>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </header>&nbsp; &nbsp; <script>&nbsp; &nbsp; &nbsp; document.querySelector('.myLink').addEventListener('click', function (e) {&nbsp; &nbsp; &nbsp; &nbsp; e.preventDefault();&nbsp; &nbsp; &nbsp; &nbsp; document.querySelector('.inner-showcase').style = 'transform: scale(1.2)';&nbsp; &nbsp; &nbsp; &nbsp; setTimeout(() => location.href = this.href, 1000);&nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; </script>&nbsp; </body></html>注意:您应该将元素移动到您的<header />内部<body />而不是元素内部<head />。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript