2017-04-06 5 views
0

을 내 작품에 대해 패키지로 적습니다. 이 같은공백이나 따옴표를 덜 수정하여 임의의 키 프레임 이름을 만드는 방법

일부 코드 :

.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; 
 
}
그때 어디 선가 내가 필요로 그것을 사용할 수 있습니다. 이처럼

:

.testbox{ 
 
    .goTop(1s); 
 
}

왜 내가 임의의 숫자를 사용해야합니까?

, 때문에 모든 키 프레임은 같은 이름, 그리고 마지막 부분은 오래된 부분을 덮어 쓰게됩니다. (인수가 동일하지 않습니다)

지금, 키 프레임 이름은 임의의 숫자 인 경우.

그러나 키 프레임 이름이 숫자 인 경우 애니메이션이 실행되지 않습니다.

goTop_12345678과 같은 이름으로 바란다.

변수 이름의 문자열에 문의하려고합니다. 그래서

@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.
,

...이 문제를 해결하는 방법

...

고마워요. @ 일곱 단계 - 최대에 대한

@cloudhead

+0

문제는 기본적으로 "이하의 문자열을 연결하는 방법"을 참조하십시오. [예] (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 % 40Nrandom % 3A % 20 % 60Math.round (Math.random() % 20 * % 201.0e8) % 60 % 3B % 5Cn % 20 % 20 % 20 % 20 % 40 % % 3A % 20 % % 5C % 22go-top- % 40 % 7Brandom % 7D % 5C % 22 % 3B % 20 % 2F % 2F % 20 % 3C- % 20here % 20we % 20go % 5Cn % 5Ct % 40 키 프레임 % 20 % 40 % 20 % 7B % 5Cn % 5Ct % 5Ctfrom % 20 % 7Bbar % 20 % 3A1 % 7D % 5Cn % 5Ct % 5Ctto % 20 % 20 % 20 % 7Bbaz % 3A % 202 % 7D % 5Cn % 5Ct % 7D % 5Cn % 5Ctanimation-name % 3A % 20 % 40name % 3B % 5Cn % 7D % 5Cn % 22 % 7D). –

+0

구체적인 경우에는 [just ...] (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 % 40 % % 3A % 20 % % 60'go-top '% 20 % 2B % 20Math. round (Math.random() % 20 * % 201.0e8) % 60 % 3B % 5Cn % 5Ct % 40 키 프레임 % 20 % 40 % 20 % 7B % 5Cn % 5Ct % 5Ctfrom % 20 % 7Bbar % 20 % 3A1 % 7D % 5Cn % 5Ct % 5Ctto % 20 % 20 % 20 % 7Bbaz % 3A % 202 % 7D % 5Cn % 5Ct % 7D % 5Cn % 5Ctanimation-name % 3A % 20 % 40 % % 3B % 5Cn % 7D % 5Cn % 22 % 7D). 일반적으로 그들은 일반적으로 무작위적인 hackery 대신에 mixin arg로 이름을 붙이기도합니다 (그냥 휴 ...). –

+0

고맙습니다. 당신 말이 맞습니다. – iRuxu

답변

0

마지막 솔루션이 같은 일부 코드 :

.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

감사합니다.

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

 
//or 
 

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

그러나 새로운 문제는 임의의 숫자가 동일하지 않은 것입니다 ... :

var에를 작성하는 올바른 방법은 다음과 같이이다

.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

"하지만 새로운 문제는 임의의 숫자가 동일하지 않다는 것입니다." - 네, 게으른 평가 때문입니다. 변수 평가를 단일 지점으로 강제 적용하기 위해 또 다른 해킹을 도입하는 것은 문제가 아니지만 명시 적으로 이름을 쓰는 것이 훨씬 쉽습니다. 예 : 예를 들어, 당신은 이미'.textbox'라는 고유 한 ID를 가지고 있습니다. (어쨌든 하나 이상의 애니메이션을 가질 수 없습니다.) 그래서 .goTop (textbox-animation, ...); 더 안전하고 자명하다. –

+0

[해킹] (http://less2css.org/#%7B%22less%22%3A%22%5Cnfoo%20%7B%5Cn%5Ct.go-top() % 3B % 20 % 20 % 5Cn % 5 % 20 % 20 % 20 % 20 % 20 % 20 % 20 % 20 % 20 % 20 % 20 % 20 % 20 % 20 % 20 % 20 % 20 % 20 % 20 % 20 % 20 % 5Ct.go-top (~ % 60'go-top - '% 20 % 2B % 20Math.round (수학 20 % 20 % % 201.0e8) % 60) % 3B % 5Cn % 7D % 5Cn % 5Cn. (% 40name) % 20 % 7B % 5Cn % 5Ct % 40 키 프레임 % 20 % 40 % % 20 % 7B % 5Cn % 5Ct % 5Ctfrom % 20 % 7Bbar % 3A % 201 % 7D % 5Cn % 5Ct % 5Ctto % 20 % 20 % 20 % 7Bbaz % 3A % 202 % 7D % 5Cn % 5Ct % 7D % 5Cn % 5Ctanimation-name % 3A % 20 % 40name % 3B % 20 % 20 % 5Cn % 7D % 5Cn % 22 % 7D). –

+0

대단히 감사합니다 !! – iRuxu