多个元素的jQuery颜色循环动画

时间:2017-08-26 16:59:42

标签: jquery

我已经使用下面的信息片制作了颜色循环,并且我已经将它用于我的h1标题和我的按钮。

然而,两个元素之间存在轻微的延迟,我希望同时发生色彩循环。

我不熟悉JS / JQuery,但是可以将2合并为1,以便颜色循环在元素之间同步,以及删除一些重复的代码吗?

jQuery(document).ready(function( $ ){

// Text Colour Cycling 
$('.colours h1').each(function(){
    var $this = $(this),
        colors = ['#ec008c', '#00bcc3', '#5fb26a', '#fc7331'];

    setInterval(function(){
        var color = colors.shift();
        colors.push(color);
        $this.animate({color: color}, 1500);
    },4000);
});

// Button BG Colour Cycling 
$('.colours a.button').each(function(){
    var $this = $(this),
        colors = ['#ec008c', '#00bcc3', '#5fb26a', '#fc7331'];

    setInterval(function(){
        var color = colors.shift();
        colors.push(color);
        $this.animate({backgroundColor: color}, 1500);
    },4000);
});

});

1 个答案:

答案 0 :(得分:0)

我希望这段代码能够以更加同步和简洁的方式完成您的需求。我用.css()而不是.animate()测试了这个,因为后者不适合我,但问题和答案都是有效的。

import java.net.URL;
import java.util.LinkedList;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.chart.AreaChart;
import javafx.scene.chart.XYChart;
import javafx.scene.chart.XYChart.Series;
import javafx.scene.control.TextField;


/**
 *
 * @author blj0011
 */
public class SampleController implements Initializable {

    @FXML
    private AreaChart<Number, Number> areaChart;
    @FXML
    private TextField txtSt;
    @FXML
    private TextField txtEt;
    @FXML
    private TextField txtNb;


    LinkedList<XYChart.Series<Number, Number>> seriesContainer = new LinkedList<Series<Number, Number>>();

        //Button add functionality
        @FXML
        private void generateGraph() {

            Double start = Double.parseDouble(txtSt.getText());

            Double end = Double.parseDouble(txtEt.getText());
            double numberEmployees = Integer.parseInt(txtNb.getText());

            XYChart.Series<Number, Number> series= new XYChart.Series<>();

            for (int i = start.intValue(); i <= end.intValue(); i++) {
                series.getData().add(new XYChart.Data(i, numberEmployees));
            }


           // Add Series to series container.
           seriesContainer.add(series);

           //Add only new series to AreaChart
           for(XYChart.Series<Number, Number> entry : seriesContainer)
           {
               if(!areaChart.getData().contains(entry))
               {                    
                    areaChart.getData().add(entry);
                    entry.setName("XYChart.Series "+seriesContainer.size());               }
           }
        }

        //Button delete functionality
        @FXML
        private void deleteGraph() {

        }

        //Button Undo functionality
        @FXML
        private void undoGraph(){

            }

        //Button Save functionality
        @FXML
        private void saveGraph(){           
                int max = 0;

                for(int i =0; i< seriesContainer.size(); i++){
                    XYChart.Series<Number, Number> test = seriesContainer.get(i);
                        System.out.println("Series "+i+" Employees: "+test.getData().get(i).getYValue().intValue());
                        System.out.println("Series "+i+" Start: "+test.getData().get(i).getXValue().intValue());

                        // find maximal y value
                        int x = test.getData().get(i).getYValue().intValue();
                        if (x > max) {
                            max = x;
                        }
                        System.out.println("Series "+i+" End: "+max);
                }
            }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        areaChart.setTitle("Chronos");
        areaChart.getXAxis().setLabel("Heures");
        areaChart.getYAxis().setLabel("Employés");
    }
}