Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.7k views
in Technique[技术] by (71.8m points)

javascript - Using anime.js and media queries for responsive design

Hello everyone!
I have been working on this logo-click animation using anime.js and finally got it down for desktop view, but later realized that I should have done mobile first because now I am having issues with the animation and a media query.
(This example may look a little wonky, but you'll get the idea)
The resizing of the logo is off when switching to a smaller screen, so I tried using an if/else for doing a different animation style on a smaller screen. How can I make it over write the animation for desktop to mobile
Thanks a bunch!

var gitLogo = document.getElementById('github');
var linkLogo = document.getElementById('linkedin');
var codeLogo = document.getElementById('codepen');
var twLogo = document.getElementById('twitter');

const logoClick =
logo.addEventListener('click', () => {
  
  var tl =
 anime.timeline({
   targets: '.logo-default',
   width: '5rem',
   height: '5rem', 
   duration: 300
 });
  tl
  .add({
    targets: gitLogo,
    height: 40,
    translateX: -85 
    })
  .add({
    targets: linkLogo,
    height: 30,
    translateX: -80,
    translateY: 50
    })
  .add({
    targets: codeLogo,
    height: 25,
    translateX: -50,
    translateY: 70
    })
  .add({
    targets: twLogo,
    height: 23,
    translateX: -12,
    translateY: 75
    });

});

const logoClickMediaQ =
logo.addEventListener('click', () => {
  
  var tl =
 anime.timeline({
   targets: '.logo-default',
   width: '5rem',
   height: '5rem', 
   duration: 300
 });
 tl
  .add({
    targets: gitLogo,
    height: 40,
    translateX: -85 
    })
  .add({
    targets: linkLogo,
    height: 30,
    translateX: -80,
    translateY: 50
    })
  .add({
    targets: codeLogo,
    height: 25,
    translateX: -50,
    translateY: 70
    })
  .add({
    targets: twLogo,
    height: 23,
    translateX: -12,
    translateY: 75
    });

});
//logo click for small screen

const mediaQ = window.matchMedia("(min-width: 600px)");

if (mediaQ.matches) {
logoClickMediaQ;
} else {
  logoClick;
};
body {
    display:flex;
    flex-direction: column;
}
.logo-default{
    height: 100px;
    width: auto;
    padding-top: 20px;  
  }
  
#info-corner {
    margin-left: auto;
}

.icon {
    height: 10px;
    width: auto;
    position: absolute;
    top: 40px;
    right: -4px;
}

@media only screen and (max-width: 600px) {
    #info-corner {
    margin-top: 70rem;
    width: 100%;
    padding-left: 35rem;
    padding-bottom: 10rem;
}

#logo {
    width: 30rem;
    height: auto;
    position: relative;
}

#contact-tab {
    position: absolute;
    width: 20rem;
    height: 10rem;
    
}

.icon {
    width: 8rem;
    height: auto;
    position: absolute;
}

}
<script src="https://cdn.jsdelivr.net/npm/animejs@3.0.1/lib/anime.min.js"></script>
<body>
<div id="info-corner">
  <img id="logo" class="logo-default logo-small-screen" src="https://i.pinimg.com/originals/c6/52/32/c65232d66c23b9a1924665956ede7347.jpg">
     <div id="contact-tab">
       <a href="#"><img id="github" class="icon" src="https://www.flaticon.com/svg/static/icons/svg/733/733609.svg" alt="github"></a>
       <a href="#"><img img id="linkedin" class="icon"     src="https://www.flaticon.com/svg/static/icons/svg/49/49408.svg" alt="linkedin icon"></a>
       <a href="#"><img img id="codepen" class="icon" src="https://www.flaticon.com/svg/static/icons/svg/2111/2111341.svg" alt="codepen icon"></a>
       <a href="#"><img img id="twitter" class="icon" src="https://www.flaticon.com/svg/static/icons/svg/733/733635.svg" alt="twitter icon"></a>
      </div>
</div>
</body>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...