2016-06-03 4 views
1

나는 이것으로별로 좋지 않아서 이것을 이해할 수 없습니다. 3 개의 버튼과 3 개의 숨겨진 div가 있습니다. 버튼을 클릭하면 숨겨진 내용이 표시됩니다. 내용의 시작 부분이 페이지 상단에 있도록 페이지를 아래로 스크롤 할 수 있습니까?숨겨진 div가 표시 될 때 콘텐츠로 스크롤

<script type="text/javascript"> 
function showDiv(num) { 
    document.getElementById('div1').style.visibility='hidden'; 
    document.getElementById('div1').style.height='0px'; 
    document.getElementById('div1').style.overflow='hidden'; 
    document.getElementById('div2').style.visibility='hidden'; 
    document.getElementById('div2').style.height='0px'; 
    document.getElementById('div2').style.overflow='hidden'; 
    document.getElementById('div3').style.visibility='hidden'; 
    document.getElementById('div3').style.height='0px'; 
    document.getElementById('div3').style.overflow='hidden'; 
    document.getElementById('div'+num).style.visibility='visible'; 
    document.getElementById('div'+num).style.height='10px'; 
    document.getElementById('div'+num).style.overflow='visible';  
} 
</script> 

내 CSS는 :

#div1, #div2, #div3 { 
    visibility: hidden; 
    height:0px; 
    overflow:hidden 

} 

.button { 
    background-color: green; 
    border: none; 
    color: white; 
    padding: 15px 32px; 
    text-align: center; 
    text-decoration: none; 
    display: inline-block; 
    font-size: 22px; 
    cursor: pointer; 
    font-family: Arial; 
} 

내 버튼 :

<input type="button" class="button" name="Showdiv1" value="Show Div 1" onclick="showDiv('1')" /> 
<input type="button" class="button" name="Showdiv2" value="Show Div 2" onclick="showDiv('2')" /> 
<input type="button" class="button" name="Showdiv3" value="Show Div 3" onclick="showDiv('3')" /> 
<div id="div1"> I'm div1 </div> 
<div id="div2"> I'm div2 </div> 
<div id="div3"> <form style="margin-top:1000px;height: 1000px;" id="form1"> 
     Form with huge data in it 
     </form> </div> 

DIV 3 페이지 아래 더 시작하는 긴 형식이있는 데모 : https://jsfiddle.net/cgrouge/Lx0pyg4L/

+0

당신은 내가 같은 것을 사용하는 것이 좋습니다 것입니다 jQuery를 사용한다면 : 모든보다는하는'selected' 클래스를 사용, (먼저 선택한 내용의 위쪽 여백을 하차 JS 스타일 변경) 페이지의 해당 위치로 스크롤합니다 ('$ ('html, body'). scrollTop (선택한 항목 상단 여백))) – DBS

답변

1
$(function() { 
$('a[href*="#"]:not([href="#"])').click(function() { 
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) { 
    var target = $(this.hash); 
    target = target.length ? target : $('[name=' + this.hash.slice(1) +']'); 
    if (target.length) { 
    $('html, body').animate({ 
     scrollTop: target.offset().top 
    }, 1000); 
    return false; 
    } 
} 
}); 
}); 

편집 1 :

Reference 이 스크립트를 사용할 수 있습니다. 그것은 당신의 목적을 위해 완벽하게 작동합니다. 입력 태그를 앵커 태그 안에 넣어야합니다.

편집 2 :

Pen

+0

그러면 href 링크를 내 버튼에 포함시킬 수 있습니까? 클릭시 내 숨겨진 div를 공개 하시겠습니까? – cgrouge

+0

답변에 codepen 링크를 추가했습니다. 그것을 확인할 수 있습니다. div3를 클릭하면 원하는 효과를 볼 수 있습니다. 앵커 태그를 추가하더라도 – Pushpendra

+0

을 클릭하지 않으면 div가 표시되지 않습니다. 잘 작동합니다. 감사합니다. div3 콘텐츠로 이동하는 대신 페이지 스크롤을 볼 수 있도록 애니메이션 속도를 느리게 할 수 있습니까? – cgrouge

관련 문제