Javascript 从右向左的过渡

Javascript 从右向左的过渡,javascript,html,css,Javascript,Html,Css,我需要通过从右到左的平滑过渡来改变一些背景色,而不是仅仅以统一的方式改变颜色。我现在将向您提供我的代码,这样可能会更清楚 不允许使用jQuery/库 HTML Javascript function getRandomInt(max) { return Math.floor(Math.random() * Math.floor(max)); } function gestoreGiro () { bott.disabled= true; bott.setAttribut

我需要通过从右到左的平滑过渡来改变一些背景色,而不是仅仅以统一的方式改变颜色。我现在将向您提供我的代码,这样可能会更清楚

不允许使用jQuery/库

HTML

Javascript

function getRandomInt(max) {
  return Math.floor(Math.random() * Math.floor(max));
}



function gestoreGiro () {
    bott.disabled= true;
    bott.setAttribute("style", "background:gray")
    t = setInterval(scorri, 500);
    setTimeout(riattiva, 5000);
}

function riattiva() {
    document.getElementById('roll').disabled = false;
    bott.setAttribute("style", "background:none");
}

function scorri() {
    var n = i - 1;
    var lunghezza = listaColori["colori"].length;
    const valore = getRandomInt(listaColori["colori"].length);


    if (i>listaColori["colori"].length-1) {
        i = 0;
    }    
    if (n < 0) {
        n = lunghezza-1;
    }    
    if (m>listaColori["colori"].length-1) {
        m = 0;
    }

    S1.setAttribute("style","background:"+listaColori["coloriHEX"][n]);
    S2.setAttribute("style","background:"+listaColori["coloriHEX"][i]);
    S3.setAttribute("style","background:"+listaColori["coloriHEX"][m]);
    i++;
    m++;
    flag++;

    if (flag==13) {
        clearInterval(t);
        n = valore-1;
        m = valore +1;
        if (n < 0) {
            n = lunghezza-1;
        }
        if (m>lunghezza-1) {
            m = 0;
        }

        S1.setAttribute("style","background:"+listaColori["coloriHEX"][n]);
        S2.setAttribute("style","background:"+listaColori["coloriHEX"][valore]);
        S3.setAttribute("style","background:"+listaColori["coloriHEX"][m]);

        flag = 0;
    }
}

var S1 ;
var S2 ;
var S3 ;
var bott;
var i = 0;
var m = i+1;
var t;
var flag = 0;


function gestoreLoad () {

    S1 = document.getElementById("color1") ;
    S2 = document.getElementById("color2") ;
    S3 = document.getElementById("color3") ;
    bott = document.getElementById("roll");
    bott.onclick = gestoreGiro;  

}


window.onload = gestoreLoad;

var listaColori = {
    colori: ["verde", "giallo", "rosso", "blu", "nero", "bianco"],
    coloriHEX: ["#2e8b57", "#ffd700", "#ff6347", "#4169e1", "#000000", "#ffffff"]
}
函数getRandomInt(最大值){
返回Math.floor(Math.random()*Math.floor(max));
}
函数gestoreGiro(){
bott.disabled=true;
bott.setAttribute(“样式”,“背景:灰色”)
t=设定间隔(scorri,500);
设置超时(RIATIVA,5000);
}
函数tiva(){
document.getElementById('roll')。disabled=false;
bott.setAttribute(“样式”、“背景:无”);
}
函数scorri(){
var n=i-1;
var lunghezza=listaColori[“colori”]。长度;
const valore=getRandomInt(listacoli[“colori”].length);
如果(i>listaColori[“colori”].长度-1){
i=0;
}    
if(n<0){
n=lunghezza-1;
}    
如果(m>listaColori[“colori”]。长度-1){
m=0;
}
S1.setAttribute(“样式”、“背景:”+listaColori[“coloriHEX”][n]);
S2.setAttribute(“样式”、“背景:”+listaColori[“coloriHEX”][i]);
S3.设置属性(“样式”、“背景:+listaColori[“coloriHEX”][m]);
i++;
m++;
flag++;
如果(标志==13){
净间隔(t);
n=valore-1;
m=谷值+1;
if(n<0){
n=lunghezza-1;
}
如果(m>lunghezza-1){
m=0;
}
S1.setAttribute(“样式”、“背景:”+listaColori[“coloriHEX”][n]);
S2.setAttribute(“样式”、“背景:”+listaColori[“coloriHEX”][valore]);
S3.设置属性(“样式”、“背景:+listaColori[“coloriHEX”][m]);
flag=0;
}
}
var-S1;
var-S2;
var-S3;
var bott;
var i=0;
var m=i+1;
变量t;
var标志=0;
函数gestoreLoad(){
S1=document.getElementById(“color1”);
S2=document.getElementById(“color2”);
S3=document.getElementById(“color3”);
bott=document.getElementById(“roll”);
bott.onclick=gestoreGiro;
}
window.onload=gestoreLoad;
var listaColori={
colori:[“维德”、“吉亚洛”、“红牛”、“布鲁”、“尼禄”、“比安科”],
coloriHEX:[“2e8b57”、“ffd700”、“ff6347”、“4169e1”、“000000”、“ffffff”]
}

如果我的解释不好,我很抱歉:我很乐意回答您的疑问。

您可以通过css转换非常简单地做到这一点。此方法适用于:悬停。工作

HTML

<div>
  <p>This is some text</p>
</div>
function getRandomInt(max) {
  return Math.floor(Math.random() * Math.floor(max));
}



function gestoreGiro () {
    bott.disabled= true;
    bott.setAttribute("style", "background:gray")
    t = setInterval(scorri, 500);
    setTimeout(riattiva, 5000);
}

function riattiva() {
    document.getElementById('roll').disabled = false;
    bott.setAttribute("style", "background:none");
}

function scorri() {
    var n = i - 1;
    var lunghezza = listaColori["colori"].length;
    const valore = getRandomInt(listaColori["colori"].length);


    if (i>listaColori["colori"].length-1) {
        i = 0;
    }    
    if (n < 0) {
        n = lunghezza-1;
    }    
    if (m>listaColori["colori"].length-1) {
        m = 0;
    }

    S1.setAttribute("style","background:"+listaColori["coloriHEX"][n]);
    S2.setAttribute("style","background:"+listaColori["coloriHEX"][i]);
    S3.setAttribute("style","background:"+listaColori["coloriHEX"][m]);
    i++;
    m++;
    flag++;

    if (flag==13) {
        clearInterval(t);
        n = valore-1;
        m = valore +1;
        if (n < 0) {
            n = lunghezza-1;
        }
        if (m>lunghezza-1) {
            m = 0;
        }

        S1.setAttribute("style","background:"+listaColori["coloriHEX"][n]);
        S2.setAttribute("style","background:"+listaColori["coloriHEX"][valore]);
        S3.setAttribute("style","background:"+listaColori["coloriHEX"][m]);

        flag = 0;
    }
}

var S1 ;
var S2 ;
var S3 ;
var bott;
var i = 0;
var m = i+1;
var t;
var flag = 0;


function gestoreLoad () {

    S1 = document.getElementById("color1") ;
    S2 = document.getElementById("color2") ;
    S3 = document.getElementById("color3") ;
    bott = document.getElementById("roll");
    bott.onclick = gestoreGiro;  

}


window.onload = gestoreLoad;

var listaColori = {
    colori: ["verde", "giallo", "rosso", "blu", "nero", "bianco"],
    coloriHEX: ["#2e8b57", "#ffd700", "#ff6347", "#4169e1", "#000000", "#ffffff"]
}
<div>
  <p>This is some text</p>
</div>
div {
  padding: 20px;
  width: 100px;
  background: linear-gradient( to left, red 50%,     blue 50% );
  background-size: 200% 100%;
  background-position: right bottom;
  transition: all ease 1s;
  color: white;
}

div:hover {
  background-position: left bottom;
}