2016-11-20 1 views
-1

그래,이 자바 스크립트가 있었고 완벽하게 작동했습니다.스크롤의 요소 모양을 변경하는 JavaScript

var header = document.getElementById('header'); 
 

 
window.onscroll = function() { 
 
    "use strict"; 
 
    if (document.body.scrollTop >= 6) { 
 
     header.className = 'header-colored'; 
 
    } else { 
 
     header.className = 'header-transparent'; 
 
    } 
 
};
하지만 스크립트에 다른 요소를 추가 할 때, 그것은 어떤 이유로 작동을 멈췄습니다. 머리글에서와 같이 작동이 중지되면 투명하고 화살표가 항상 표시됩니다.

모든 관련 코드 (내가 믿는) : 스크립트 내가 새로운 변수 및 코드의 두 개의 여분의 라인을 추가 할 때 작동이 중지 이유

var header = document.getElementById('header'); 
 
var arrow = document.getElementsById('arrowToTop'); 
 
window.onscroll = function() { 
 
    "use strict"; 
 
    if (document.body.scrollTop >= 6) { 
 
     header.className = 'header-colored'; 
 
     arrow.className = 'arrow-visible'; 
 
    } else { 
 
     header.className = 'header-transparent'; 
 
     arrow.className = 'arrow-transparent'; 
 
    } 
 
};
#header { 
 
    position: fixed; 
 
} 
 

 
.header-transparent { 
 
    background-color: transparent; 
 
} 
 

 
.header-colored { 
 
    background-color: black; 
 
    opacity: .9; 
 
} 
 

 
.toTop { 
 
    bottom: 0; 
 
    right: 0; 
 
    position: fixed; 
 
} 
 

 
    .arrow-visible { 
 
    display: block; 
 
} 
 

 
.arrow-transparent { 
 
    display: none; 
 
}
<header id="header" class="header-transparent"> 
 
     <img src="Bilder/LOGO.png" alt="Blabla" style="width: 10%;"> 
 
     <ul id="navbar"> 
 
      <li><a href="#allText">home</a></li> 
 
      <li><a href="#">business</a></li> 
 
      <li><a href="#">technical</a></li> 
 
      <li><a href="#">about us</a></li> 
 
     </ul> 
 
    </header> 
 

 
    </div> 
 
    <i class="material-icons" id="arrowToTop"><a class="toTop" href="#">keyboard_arrow_up</a></i> 
 
    </div>

사람이 이유를 알고 있나요? 감사합니다.

+0

브라우저 검사기 콘솔에 오류가 있습니까? – mauris

+0

아니, 한 글자가 너무 많아서 문제가되었다 :) – r4tchet

답변

0

신고 내용은 arrow입니다. 이

var arrow = document.getElementsById('arrowToTop'); 

var arrow = document.getElementById('arrowToTop'); 

에 당신이 그것에 대해 오류가 발생하지 않았다 표시되지 않습니다 변경합니다. JS 함수가 아닙니다.

+0

쉽게 그걸 :) 감사합니다! 'arrow.className' to 'arrow.id'을 변경 하시겠습니까? – r4tchet

+0

기꺼이 도와 드리겠습니다. 문제가 해결 되었다면 답을 표시하십시오. 아니요, 요소의 CSS 클래스를 사용하고 있으므로 클래스를 className 객체가 아닌 ID로 설정해야합니다. – TheValyreanGroup

+0

예, 지금 표시하십시오! 그리고 예, 나는 물론 클래스에서 ID로 CSS를 바꿀 것입니다. 클래스를 사용할 때의 문제점은 아이콘을 올바르게 표시하기 위해 요소에 'material-icons' 클래스가 있어야한다는 것입니다. 화살표 아이콘을 변경하면 arrow_up_icon이라는 텍스트가 표시됩니다. – r4tchet

관련 문제