2013-12-22 6 views
-1

안녕하세요, 나는 친구를 찾고 있습니다. 그녀는 매 계절마다 다른 스타일 시트를 사용하려고합니다. 하나는 겨울, 봄, 여름, 가을.계절에 따라 사이트의 스타일이 다릅니다

다음 접근 방식에 대해 생각해 봤습니다. 각 계절마다 style.css를 사용하여 사이트를 구축했습니다. 이제는 해당 기간 동안 적절한 style.css를로드해야합니다. .

하지만 어떻게 할 수 있습니까?

+0

당신은 몇 가지 검사를 수행하기 위해 PHP와 같은 서버 측 언어를 사용해야합니다 날짜와 적절한 CSS를 호출 –

+1

@ Matías 아니, 그럴 필요가 없어 – scrblnrd3

+0

@ scrblnrd3, 잘 안돼; 서버 측 일 필요는 없지만 시즌을 감지하기 위해서는 (클라이언트 측 또는 서버 측) 완료해야하며 날짜를 평가하여 완료해야합니다. 그러나 관계없이 JavaScript 또는 일부 서버 측 솔루션 (선택 또는 사용 가능)이 없으면 이것이 불가능합니다. –

답변

1

당신은 분명히

var month=new Date().getMonth(); 
if(month<2 || month==11){ 
    setActiveStyleSheet("winter"); 
}else if(month<5){ 
    setActiveStyleSheet("spring"); 
}else if(month<8){ 
    setActiveStyleSheet("summer"); 
}else if(month<11){ 
    setActiveStyleSheet("fall"); 
} 

을 사용할 수 있습니다, 시즌을 감지 한 후 다른 스타일

<link rel="stylesheet" type="text/css" title="summer" href="summer.css"> 
<link rel="alternate stylesheet" type="text/css" title="spring" href="spring.css"> 
<link rel="stylesheet" type="text/css" title="fall" href="fall.css"> 
<link rel="alternate stylesheet" type="text/css" title="winter" href="winter.css"> 

을 사용 그리고 스크립트

function setActiveStyleSheet(title) { 
    var links=document.getElementsByTagName("link"); 
    for(var i=0; i<styles.length; i++) { 
    style=styles[i]; 
    if(style.getAttribute("rel").indexOf("style") != -1 && style.getAttribute("title")) { 
     style.disabled = true; 
     if(style.getAttribute("title") == title){ 
      style.disabled = false; 
     } 
    } 
    } 
} 

를 작성하고 수, 당신은 할 수 있습니다 계절 선택이 더 정확하지만, 그것은 적어도 당신에게 기초적인 계절을 줄 것입니다

+0

고맙습니다 .. 그게 빠르다. 나는 그 옵션을 살펴볼 것이다. 고맙습니다. – XpDieto

+0

그가 summer.css를 선택하는 부분까지 작동하는이 제품은 무엇인가 놓치고 있습니까? – XpDieto

+0

webdomein에서 이것을 테스트 해 보았습니까? 또는 이것도 오프라인에서 작동해야합니까? – XpDieto

2

사용자가 자바 스크립트를 비활성화했을 수 있으므로 PHP를 사용하는 것이 좋습니다.

이 내 방법이 될 것입니다 :

PHP :

//season.php file 
header("Content-type: text/css"); 

function getSeason() 
{ 
    $season_dates = array('/12/21'=>'winter', 
         '/09/21'=>'autumn', 
         '/06/21'=>'summer', 
         '/03/21'=>'spring', 
         '/12/31'=>'winter'); 

    foreach ($season_dates as $key => $value) // Loop through the season dates 
    { 
     $season_date = date("Y").$key; 
     if (strtotime("now") > strtotime($season_date)) // If we're after the date of the starting season 
     { 
      return $value; 
     } 
    } 
} 

$season = getSeason(); 

echo file_get_contents("$season.css"); 

HTML :

<link rel="stylesheet" type="text/css" href="season.php"> 
+0

이 접근법은 짧습니다. 나는 그것을 좋아한다. 하지만 작동하지 않는 것 같습니다. PHP 파일을 만들었습니다. html로 링크했습니다. like suggest 뭔가를 놓친 건가요? – XpDieto

+0

수정 : 헤더 추가 ("Content-type : text/css");' –

관련 문제