2017-04-06 1 views
0

Ich habe ein weniger Paket für meine Arbeit schreiben.So erstellen Sie einen zufälligen Keyframes-Namen um weniger das Leerzeichen oder das Anführungszeichen

Einige Code wie folgt:

.goTop(@time:1s,@position:-100%){ 
 
\t @random_suffix:`Math.round(Math.random() * 1.0e8)`; 
 
\t @keyframes @random_suffix{ 
 
\t \t from{transform:translateY(0);} 
 
\t \t to{transform:translateY(@position);} 
 
\t } 
 
\t animation-name:@random_suffix; 
 
\t animation-duration:@time; 
 
\t animation-timing-function:ease-out; 
 
\t animation-fill-mode:forwards; 
 
}

Dann kann ich es irgendwo verwenden, um auf die ich brauche.

So:

.testbox{ 
 
    .goTop(1s); 
 
}

Warum sollte ich eine Zufallszahl verwenden?

Denn wenn alle Keyframes sind die gleichen Namen, und der letzte Teil wird den alten Teil überschrieben werden. (Die Argumente sind nicht die gleichen)

Nun wird der Keyframe-Name eine Zufallszahl ist.

Wenn der Keyframe-Name jedoch eine Zahl ist, wird die Animation nicht ausgeführt.

Also ich hoffe, der Name wie goTop_12345678.

Ich versuche, die Zeichenfolge für den Variablennamen zu kontaktieren.

@temp1:goTop_; 
 
@temp2:`Math.round(Math.random() * 1.0e8)`; 
 
@test:@temp1 + @temp2; 
 
// Error 
 
//{"type":"Syntax","message":"Cannot read property 'numerator' of undefined","filename":"input","index":94,"line":4,"callLine":null,"column":0,"extract":["@temp2:goTop_;","@test:@temp1 + @temp2;",""]} 
 

 
@temp1:'goTop_'; 
 
@temp2:`Math.round(Math.random() * 1.0e8)`; 
 
@test:`@{temp1} + @{temp2}`; 
 
//"goTop_12345678" 
 
//It contains the symbol quote "",and the animation will not run. 
 

 
@temp1:goTop_; 
 
@temp2:@temp1+`Math.round(Math.random() * 1.0e8)`; 
 
@test:@temp2; 
 
//"goTop_ 12345678" 
 
//It contains the space ,and the animation will not run too. 
 

 
//For the second and third way 
 
//I have try to use replace way to solve it,but I failed.

so, ...

Wie dieses Problem ...

Vielen Dank zu lösen.

@cloudhead

+0

Die Frage ist im Grunde "Wie Zeichenfolge in weniger verketten". [Beispiel] (http://less2css.org/#%7B%22less%22%3A%22%5Cnfoo%20%7B%5Cn%5Ct.go-top()% 3B% 20% 20% 5Cn% 7D% 5Cn% 5Cn.go-top()% 20% 7B% 5Cn% 5Ct% 40Rand% 3A% 20% 60Math.round (Math.Random()% 20 *% 201.0e8)% 60% 3B% 5Cn% 20% 20 % 20% 20% 40name% 3A% 20 ~% 5C% 22go-top-% 40% 7Brandom% 7D% 5C% 22% 3B% 20% 2F% 2F% 20% 3C-% 20here% 20we% 20go% 5Cn% 5Ct% 40Keyframes% 20% 40Name% 20% 7B% 5Cn% 5Ct% 5Ctvon% 20% 7Bbar% 20% 3A1% 7D% 5Cn% 5Ct% 5Ctto% 20% 20% 20% 7Bbaz% 3A% 202% 7D% 5Cn% 5Ct% 7D% 5Cn% 5Chanimationsname% 3A% 20% 40Name% 3B% 5Cn% 7D% 5Cn% 22% 7D). –

+0

Für Ihren speziellen Fall können Sie es machen [nur ...] (http://less2css.org/#%7B%22less%22%3A%22%5Cnfoo%20%7B%5Cn%5Ct.go-top ()% 3B% 20% 20% 5Cn% 7D% 5Cn% 5Cn.go-oben()% 20% 7B% 5Cn% 5Ct% 40Name% 3A% 20 ~% 60'go-top '% 20% 2B% 20Math. Runde (Math.random()% 20 *% 201.0e8)% 60% 3B% 5Cn% 5Ct% 40Keyframes% 20% 40Name% 20% 7B% 5Cn% 5Ct% 5Ctvon% 20% 7Bbar% 20% 3A1% 7D% 5Cn % 5Ct% 5Ctto% 20% 20% 20% 7Bbaz% 3A% 202% 7D% 5Cn% 5Ct% 7D% 5Cn% 5Chanimationsname% 3A% 20% 40Name% 3B% 5Cn% 7D% 5Cn% 22% 7D). Obwohl sie im Allgemeinen den Namen als den Mixin Arg anstelle des zufälligen Hacks liefern (wie es nur Puh ist ...). –

+0

Vielen Dank..du hast Recht. – iRuxu

Antwort

0

Die letzte Lösung, einige Code wie folgt:

.goTop(@time:1s,@position:-100%,@ease:ease-out,@fillmode:forwards){ 
 
\t .goTopKeyframe(~`'goTop_'+Math.round(Math.random() * 1.0e8)`); 
 
} 
 
.goTopKeyframe(@name){ 
 
\t @keyframes @name{ 
 
\t \t from{transform:translateY(0);} 
 
\t \t to{transform:translateY(@position);} 
 
\t } 
 
\t animation-name:@name; 
 
\t animation-duration:@time; 
 
\t animation-timing-function:@ease; 
 
\t animation-fill-mode:@fillmode; 
 
}

0

Dank für die @ Sieben-Phasen-max.

der richtige Weg, um die var ist wie folgt zu schreiben:

@random: `Math.round(Math.random() * 1.0e8)`; 
 
@name: ~"[email protected]{random}"; // <- here we go 
 

 
//or 
 

 
@name:~"go-top-`Math.round(Math.random() * 1.0e8)`";

Aber das neue Problem ist, dass die Zufallszahl nicht gleich sind ...

.test { 
 
    background-color: #f39; 
 
    width: 200px; 
 
    height: 200px; 
 
    margin-top: 400px; 
 
    -webkit-animation-name: goTop_70024767; 
 
      animation-name: goTop_70024767; 
 
    -webkit-animation-duration: 1s; 
 
      animation-duration: 1s; 
 
    -webkit-animation-timing-function: ease-out; 
 
      animation-timing-function: ease-out; 
 
    -webkit-animation-fill-mode: forwards; 
 
      animation-fill-mode: forwards; 
 
} 
 
@-webkit-keyframes goTop_50030730 { 
 
    from { 
 
    -webkit-transform: translateY(0); 
 
      transform: translateY(0); 
 
    } 
 
    to { 
 
    -webkit-transform: translateY(-100%); 
 
      transform: translateY(-100%); 
 
    } 
 
} 
 
@keyframes goTop_50030730 { 
 
    from { 
 
    -webkit-transform: translateY(0); 
 
      transform: translateY(0); 
 
    } 
 
    to { 
 
    -webkit-transform: translateY(-100%); 
 
      transform: translateY(-100%); 
 
    } 
 
}

+0

"Aber das neue Problem ist, dass die Zufallszahl nicht die gleichen sind." - Ja, wegen der Lazy-Evaluation. Es ist kein Problem, einen weiteren Hack einzuführen, um die Variablenauswertung auf einen einzelnen Punkt zu zwingen - aber es ist wieder viel einfacher, den Namen explizit zu schreiben ... z. für dein Beispiel hast du bereits die eindeutige ID, die '.textbox' ist (es kann sowieso nicht mehr als eine Animation haben) ... also nur' .goTop (Textbox-Animation, ...); 'da wäre viel sicherer und selbstverständlicher. –

+0

[Der Hack] (http://less2css.org/#%7B%22less%22%3A%22%5Cnfoo%20%7B%5Cn%5Ct.go-top()% 3B% 20% 20% 5Cn% 7D% 5Cn% 5Cn.go-top()% 20% 7B% 5Cn% 20% 20% 20% 20% 2F% 2F% 20passing% 20var% 20bis% 20a% 20Mixin% 20forces% 20its% 20evaluation% 3A% 5Cn% 5Ct.go-top (~% 60'go-top - '% 20% 2B% 20Math.round (Math.random()% 20 *% 201.0e8)% 60)% 3B% 5Cn% 7D% 5Cn% 5Cn. Go-Top (% 40name)% 20% 7B% 5Cn% 5Ct% 40Keyframes% 20% 40Name% 20% 7B% 5Cn% 5Ct% 5Ctvon% 20% 7Bbar% 3A% 201% 7D% 5Cn% 5Ct% 5Ctto% 20% 20% 20% 7Bbaz% 3A% 202% 7D% 5Cn% 5Ct% 7D% 5Cn% 5Chanimationsname% 3A% 20% 40Name% 3B% 20% 20% 5Cn% 7D% 5Cn% 22% 7D). –

+0

Vielen Dank !! – iRuxu

Verwandte Themen