Как сделать так, чтобы анимация CSS появлялась после нажатия на изображение - PullRequest
1 голос
/ 29 мая 2020

Я пытаюсь перемещать изображения с помощью CSS анимации, но после того, как я нажимаю на них. В настоящее время они перемещаются, как только я захожу на страницу. Я хочу, чтобы при нажатии на любое изображение появлялась анимация CSS. Я считаю, что вам придется сделать это с помощью кода javascript, но я не знаю, как это сделать. Я новичок в программировании и хотел бы получить любую помощь. Дайте мне знать, если вам понадобится дополнительная информация. Спасибо

var doineedtowait = 0;
function myFunction(imgs) {
  if(doineedtowait == 0){
  sleep(1000);
  }
  var expandImg = document.getElementById("expandedImg");
  expandImg.src = imgs.src;
  var imgParagraph = document.getElementById('img-paragraph');
  imgParagraph.innerHTML = imgs.getAttribute("yeep");

  expandImg.parentElement.style.display = "table-cell";
  imgParagraph.style.display = 'table-cell';
  doineedtowait++; //after executing we update the doineedtowait to +1 
  //so the if with sleep for 1 second will not be used anymore
}
function sleep(milliseconds) {
  const date = Date.now();
  let currentDate = null;
  do {
    currentDate = Date.now();
  } while (currentDate - date < milliseconds);
}
document.getElementById("myBtn").addEventListener("click", displayDate);

function displayDate() {
  document.getElementById("demo").innerHTML = Date();
}
@font-face {
  font-family: 'futuralight';
  src: url('../Fonts/Futura Light/futura_light_regular-webfont.woff2') format('woff2'), url('../Fonts/Futura Light/futura_light_regular-webfont.woff') format('woff');
  font-weight: normal;
  font-style: normal;
}

@font-face {
  font-family: 'tekoregular';
  src: url('../Fonts/Teko/teko-regular-webfont.woff2') format('woff2'), url('../Fonts/Teko/teko-regular-webfont.woff') format('woff');
  font-weight: normal;
  font-style: normal;
}

@font-face {
  font-family: 'playfair_displayregular';
  src: url('../Fonts/Playfair Display/playfairdisplay-regular-webfont.woff2') format('woff2'), url('../Fonts/Playfair Display/playfairdisplay-regular-webfont.woff') format('woff');
  font-weight: normal;
  font-style: normal;
}

@font-face {
  font-family: 'poppinsmedium';
  src: url('../Fonts/Poppins/poppins-medium-webfont.woff2') format('woff2'), url('../Fonts/Poppins/poppins-medium-webfont.woff') format('woff');
  font-weight: normal;
  font-style: normal;
}

* {
  margin: 0px;
  padding: 0px;
  border: 0px;
}

.topnav {
  background-color: rgb(84, 104, 217);
  overflow: hidden;
  font-family: 'futuralight';
  font-weight: 900;
}

.topnav a {
  float: left;
  color: #f2f2f2;
  text-align: center;
  padding: 20px 21px;
  text-decoration: none;
  font-size: 19px;
  position: relative;
  left: 2%;
}

.topnav a:before {
  content: "";
  position: absolute;
  width: 84%;
  height: 2px;
  bottom: 6px;
  left: 8%;
  background-color: white;
  visibility: hidden;
  transform: scaleX(0);
  transition: all 0.3s ease-in-out 0s;
}

.topnav a:hover:before {
  visibility: visible;
  transform: scaleX(1);
}

.topnav a.active-menu:before {
  content: "";
  position: absolute;
  width: 84%;
  height: 2px;
  bottom: 6px;
  left: 8%;
  background-color: white;
  visibility: visible;
  transform: scaleX(1);
  transition: all 0.3s ease-in-out 0s;
}

.column {
  float: left;
  width: 25%;
  padding: 10px;
}


/* Style the images inside the grid */

.column img {
  opacity: 0.85;
  cursor: pointer;
}

.column img:hover {
  opacity: 1;
}

.totalbody {
  background-color: #EEF0FC;
  height: 5000px;
}


/* Clear floats after the columns */

.row:after {
  content: "";
  display: table;
  clear: both;
}


/* The expanding image container */

#expandedImg {
  width: 40%;
  padding-top:20px;
  padding-left:20px;
}


/* Expanding image text */


/* image paragraph */

#img-paragraph {
  display: none;
  vertical-align: top;
  text-align: left;
  position: absolute;
  height: 400px;
  width: 50%;
  background-color: white;
  box-shadow: 0 2.8px 2.2px rgba(0, 0, 0, 0.034), 0 6.7px 5.3px rgba(0, 0, 0, 0.048), 0 12.5px 10px rgba(0, 0, 0, 0.06), 0 22.3px 17.9px rgba(0, 0, 0, 0.072), 0 41.8px 33.4px rgba(0, 0, 0, 0.086), 0 100px 80px rgba(0, 0, 0, 0.12);
  border-radius: 7px;
  float: right;
  right: 30px;
  top: 120px;
}

#expanded-wrapper {
  width: 100%;
}


/* Closable button inside the expanded image */

.row {
  position: absolute;
  top: 80px;
}

.yeep1 {
  position: relative;
  left: 12px;
  top: 16px;
  color: rgb(84, 104, 217);
  font-weight: 900;
  font-size: 40px;
  font-family: 'futuralight';
  text-decoration: none;
  line-height: 1;
  padding-left:4%;
  padding-top:30px;
}

.yeep2 {
  color: purple;
}

.yeep3 {
  color: green;
}
.yeep4 {
  color: blue;
}

.yeep1::before {
  content: '';
  background-color: rgb(84, 104, 217);
  width: 12px;
  height: 100%;
  position: absolute;
  right: 100%;
  bottom: -16px;
}
#column1 {
  position: relative;
  -webkit-animation: floatBubble 2s  normal ease-out;
  animation: floatBubble 2s forwards normal ease-out;
}
@-webkit-keyframes floatBubble {
  0% {
      top:00px;
  }
  100% {
      top: 500px;
  }
}
@keyframes floatBubble {
  0% {
      top:00px;
  }
  100% {
      top: 500px;
      left:50px;
  }
}
#column2 {
  position: relative;
  -webkit-animation: floatBubble 2s  normal ease-out;
  animation: floatBubble 2s forwards normal ease-out;
}
@-webkit-keyframes floatBubble {
  0% {
      top:00px;
  }
  100% {
      top: 500px;
  }
}
@keyframes floatBubble {
  0% {
      top:00px;
  }
  100% {
      top: 500px;
      left:50px;
  }
}
#column3 {
  position: relative;
  -webkit-animation: floatBubble 2s  normal ease-out;
  animation: floatBubble 2s forwards normal ease-out;
}
@-webkit-keyframes floatBubble {
  0% {
      top:00px;
  }
  100% {
      top: 500px;
  }
}
@keyframes floatBubble {
  0% {
      top:00px;
  }
  100% {
      top: 500px;
      left:50px;
  }
}
#column4 {
  position: relative;
  -webkit-animation: floatBubble 2s  normal ease-out;
  animation: floatBubble 2s forwards normal ease-out;
}
@-webkit-keyframes floatBubble {
  0% {
      top:00px;
  }
  100% {
      top: 500px;
  }
}
@keyframes floatBubble {
  0% {
      top:00px;
  }
  100% {
      top: 500px;
      left:50px;
  }
}

@media only screen and (max-width: 830px) and (min-width: 760px) {
  .topnav {
    background-color: yellow;
    overflow: hidden;
  }
}

@media only screen and (max-width: 759px) and (min-width: 646px) {
  .topnav {
    background-color: red;
    overflow: hidden;
  }
}

@media only screen and (max-width: 645px) {
  .topnav {
    background-color: black;
    overflow: hidden;
  }
}
image

Ответы [ 2 ]

2 голосов
/ 29 мая 2020

Вы можете запускать анимацию при нажатии на изображение, используя JavaScript, изменив CSS класс элемента, который вы sh анимируете:

function testFunction() {
  document.getElementById('img').classList.add('animation');
}
a {
  display: flex;
  align-items: center;
  justify-content: center;
}

.animation {
  animation: animation 1s linear infinite;
}

@keyframes animation {
  0% {
    transform: scale(1);
  }
  
  50% {
    transform: scale(2);
  
  100% {
    transform: scale(1);
  }
}
<a href="#" onclick="testFunction()">
  <img id="img" src="https://img-prod-cms-rt-microsoft-com.akamaized.net/cms/api/am/imageFileData/RE1Mu3b?ver=5c31" alt="" />
</a>

Это должно добавить анимацию к вашему элементу. Если вы хотите переключать анимацию при нажатии, просто измените classList.add('animation'); на classList.toggle('animation');. Надеюсь, это поможет.

2 голосов
/ 29 мая 2020

Вы можете поместить анимацию в отдельный класс css, а затем применить этот класс при нажатии на изображение:

в вашем. css:

#column1 {
    position: relative;
}

.animate {
    -webkit-animation: floatBubble 2s normal ease-out;
    animation: floatBubble 2s forwards normal ease-out;
}

в вашем. js:

function myFunction(imgs) {
    document.getElementById('column1').classList.add('animate');

    // your code ...
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...