简体   繁体   English

jQuery 动画背景图像超时上下浮动效果

[英]jQuery animate background image on timeout up-down float effect

I have this element with background images:我有这个带有背景图片的元素:

background-image: url(../img/Icons/about-stars-bg2.svg),
  url(../img/about-planet-1.svg), 
  url(../img/about-planet-2.svg),
  url(../img/about-planet-3.svg);
background-position: top 5px center, -115px 200px, left 1055px top 450px,
  right -45px top 350px;
background-repeat: no-repeat;
background-size: auto, auto, auto, 100px;

Now I want the planets (every picture except the first one) to move a little bit up, then down.现在我想让行星(除了第一张图片之外的每张图片)先向上移动一点,然后再向下移动。 (Float effect). (浮动效果)。

In jQuery I created a setInterval so I have a loop every 2s.在 jQuery 中,我创建了一个setInterval,所以我每 2 秒就有一个循环。 Inside I tried to change the background position.在里面我试图改变背景位置。 It works if I put如果我把它工作

.css('background-position','top 5px center, -155px 200px, left 1255px top 250px, right -15px top 350px');

But this just blinks the planets in 2 sec.但这只是在 2 秒内使行星闪烁。

Then I tried:然后我尝试:

$('.header').animate({backgroundPosition: '(0px -400px)'}, 1000 );// space here

But nothing happened.但什么也没发生。

How can I achieve the hover effect up/down ?如何实现向上/向下悬停效果? Here is the jQuery:这是jQuery:

// Move planets up down
$(document).ready(function(){
  var i = 0;
  setInterval(function(){

    //JUST BLINKS, CHANGES LOCATION
    //$('.header').css('background-position','top 5px center, -155px 200px, left 1255px top 250px, right -15px top 350px');

    //DOESN'T WORK
    $('.header').animate({backgroundPosition: '(0px -400px)'}, 1000 );

    i++;    
  },2000);   
});

 #planets { height: 90vh; background-image: url(//placehold.it/40x40/f0b), url(//placehold.it/40x40/0bf), url(//placehold.it/40x40/bf0), url(//placehold.it/40x40/0fb); background-position: 50% 15%, 25% 35%, 10% 19%, 80% 60%; background-repeat: no-repeat; background-size: 3%, 4%, 5%, 6%; animation: planets 1s 0.5s ease-in-out alternate infinite; } @keyframes planets { to { background-position: 50% 45%, 25% 65%, 10% 69%, 80% 20%; } }
 <div id="planets"> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM