Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
152 views
in Technique[技术] by (71.8m points)

javascript - scroll to top to original scroll state

I have created an html with a scrollable menu bar, consisting two headers, header 1 and header 2.

Header 1 is positioned on top of header 2. Header 1 will gradually disappear with the downward scroll and gradually reappear with the upward scroll. Header 2 will stick on top of the page as header 1 disappears, and returns to the original position (below header 1) as header 1 reappears.

I created a button that will scroll the page to the top. (not smooth scroll). The button is insider header 2. My goal is to have both header 1 and header 2 shown as the user click the scroll to top button and as the page brought back to the top.

However, my issue is that when I click the button in a state where header 1 disappears, I am only able to return to the top with only header 2 shown, and header 1 remains hidden. I would like both header 1 and header 2 to be shown instead.

Can someone please help? Thanks.

My codes are below, (please disregard the warning sign, I am new to javascript. It is not an issue that is causing the subject problem.

<script>
  window.console = window.console || function(t) {};
</script>

<script>
  if (document.location.search.match(/type=embed/gi)) {
    window.parent.postMessage("resize", "*");
  }
</script>

<script src="https://static.codepen.io/assets/common/stopExecutionOnTimeout-157cd5b220a5c80d4ff8e0e70ac069bffd87a61252088146915e8726e5d9f147.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>

<script>
window.onscroll = function() {scrollFunction()};
  var a = document.documentElement.scrollTop;
  var down1;
  var changedposition1;
  var c1;
  var e1=0;

function scrollFunction() {
  var b = document.documentElement.scrollTop;
  var down2=true;
  var changedposition2;
  var c2=down2 == b>a;
  var d;
  var e2;

  if (c2!==c1){
  changedposition2=b;
  }else{
  changedposition2=changedposition1;
  }

  d=changedposition2-b;

  e2=e1+d;

  if (e2<-68){
  e2=-68;}
  else if(e2>0){
  e2=0;}

  document.getElementById("top").style.transform = "translateY(" + (e2) + "px)";

  a=b;
  changedposition1=changedposition2;
  c1=c2;
  e1=e2;
}
</script>

<script>
function topFunction() {
  window.scrollTo({top: 0});
}
</script>

<script>
$('#newBnt').on('click', function () {
    topFunction()
});
</script>
.header1{
position:relative;
height:68px;
width:100%;
background-color:black;
}

.header2{
position:relative;
height:68px;
background-color:black;
}

#top{
position:fixed;
top:0;
width:100%;
z-index:20;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">

<div id="top">
  <div class="header1">
 </div>
 <div class="header2">
          <button id="newBnt">Go to top</button>
    </div>
  </div>
</div>

<div style="padding:15px 15px 2500px;font-size:30px">
  <p><b>s aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.</b></p>
  <p>m ipsum dolor dummy text sit amet, con</p>
  <p>m ipsum dolor dummy text sit amet, con</p>
  <p>Lorem ipsum dolor dummy text sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...