Saya telah membuat navbar yang dapat diciutkan ini yang berfungsi dengan baik,

Setiap kali scrolldown navbar tampilkan dan setiap scrolltop navbar sembunyikan

Saya ingin dengan show up navbar hide bottom terlihat oleh pengguna dapat menyembunyikan navbar .

show/hide button harus scrollUP dengan navbar dan setiap kali navar menyembunyikan tombol ini terlihat oleh pengguna !

Ada cara untuk melakukan ini?

// Hide Header on on scroll down
var didScroll;
var lastScrollTop = 0;
var delta = 5;
var navbarHeight = $('header').outerHeight();

$(window).scroll(function(event) {
  didScroll = true;
});

setInterval(function() {
  if (didScroll) {
    hasScrolled();
    didScroll = false;
  }
}, 250);

function hasScrolled() {
  var st = $(this).scrollTop();

  // Make sure they scroll more than delta
  if (Math.abs(lastScrollTop - st) <= delta)
    return;

  // If they scrolled down and are past the navbar, add class .nav-up.
  // This is necessary so you never see what is "behind" the navbar.
  if (st > lastScrollTop && st > navbarHeight) {
    // Scroll Down
    $('header').removeClass('nav-up').addClass('nav-down');
  } else {
    // Scroll Up
    if (st + $(window).height() < $(document).height()) {
      $('header').removeClass('nav-down').addClass('nav-up');
    }
  }

  lastScrollTop = st;
}
body {
  padding-top: 40px;
}

header {
  background: #f5b335;
  height: 40px;
  position: fixed;
  bottom: 0;
  transition: bottom 0.2s ease-in-out;
  width: 100%;
}

.nav-up {
  bottom: -40px;
}

main {
  background: url() repeat;
  height: 2000px;
}

footer {
  background: #ddd;
}

* {
  color: transparent
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<header class="nav-down">
  This is your menu.
</header>
<main>
  This is your body.
</main>
<footer>
  This is your footer.
</footer>

Adakah yang tahu cara yang baik untuk menulis ini? terima kasih atas bantuannya............................

0
Bijan Zand 27 Oktober 2019, 17:04

2 jawaban

Jawaban Terbaik

Periksa solusi ini.

diedit

// Hide Header on on scroll down
var didScroll;
var lastScrollTop = 0;
var delta = 5;
var navbarHeight = $('header').outerHeight();

$(window).scroll(function(event) {
  didScroll = true;
});

setInterval(function() {
  if (didScroll) {
    hasScrolled();
    didScroll = false;
  }
}, 250);

function hasScrolled() {
  var st = $(this).scrollTop();

  // Make sure they scroll more than delta
  if (Math.abs(lastScrollTop - st) <= delta)
    return;

  // If they scrolled down and are past the navbar, add class .nav-up.
  // This is necessary so you never see what is "behind" the navbar.
  if (st > lastScrollTop && st > navbarHeight) {
    // Scroll Down
    if ($('header a').hasClass('Down-Arrow')){
    $('header').removeClass('nav-up').addClass('nav-down');
    } else {
    $('.UP-Arrow').addClass('hide-arrow');
    }

  } else {
    // Scroll Up
    if (st + $(window).height() < $(document).height()) {
      $('.UP-Arrow').removeClass('hide-arrow');
      $('header').removeClass('nav-down').addClass('nav-up');
    }
  }

  lastScrollTop = st;
}

$(document).on('click','.Down-Arrow',function(){
$(this).removeClass('Down-Arrow').addClass('UP-Arrow');
$('header').removeClass('nav-down').addClass('nav-up');
});

$(document).on('click','.UP-Arrow',function(){
$(this).removeClass('UP-Arrow').addClass('Down-Arrow');
    $('header').removeClass('nav-up').addClass('nav-down');
});
body {
  padding-top: 40px;
}

header {
  background: #f5b335;
  height: 40px;
  position: fixed;
  bottom: 0;
  transition: bottom 0.2s ease-in-out;
  width: 100%;
}

header a{
color:#000;
position:absolute;
width:30px;
height:30px;
padding:5px;
background:#f5b335;
bottom:40px;
right:20px;
border-radius: 2px 2px 0 0;
text-align:center;
transition-duration:0.3s;
cursor:pointer;
}

.Down-Arrow:after{
content: "˅";
position:absolute;
top:15px;
left:15px;
}

.UP-Arrow:after{
content: "˄";
position:absolute;
top:15px;
left:15px;
}

header.nav-up .Down-Arrow{
bottom:0px;
}

.hide-arrow{
bottom: -40px;
}

.nav-up {
  bottom: -40px;
}

main {
  background: url() repeat;
  height: 2000px;
}

footer {
  background: #ddd;
}

* {
  color: transparent
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<header class="nav-down">
  This is your menu.
  <a class="Down-Arrow"></a>
</header>
<main>
  This is your body.
</main>
<footer>
  This is your footer.
</footer>
0
Ahmed Tag Amer 28 Oktober 2019, 12:33

Anda bisa melakukannya seperti ini. Saya menghapus CSS color: transparent untuk Anda lihat. Yang Anda butuhkan hanyalah menyetel tampilan navbar none ke '';

// Hide Header on on scroll down
var didScroll;
var lastScrollTop = 0;
var delta = 5;
var navbarHeight = $('header').outerHeight();

$(window).scroll(function(event) {
  didScroll = true;
});

setInterval(function() {
  if (didScroll) {
    hasScrolled();
    didScroll = false;
  }
}, 250);

function hasScrolled() {
  var st = $(this).scrollTop();

  // Make sure they scroll more than delta
  if (Math.abs(lastScrollTop - st) <= delta)
    return;

  // If they scrolled down and are past the navbar, add class .nav-up.
  // This is necessary so you never see what is "behind" the navbar.
  if (st > lastScrollTop && st > navbarHeight) {
    // Scroll Down
    $('header').removeClass('nav-up');
    $('div').removeClass('nav-up');
  } else {
    // Scroll Up
    if (st + $(window).height() < $(document).height()) {
      $('header').addClass('nav-up');
      $('div').addClass('nav-up');
    }
  }

  lastScrollTop = st;
}

$("div").click(function(){
  $('header').slideToggle('fast', function() {
  $("div").css("bottom", $("div").css("bottom") === '30px' ? '10px' : '30px');
  });
 
});
body {
  padding-top: 40px;
}

header {
  background: #f5b335;
  height: 40px;
  position: fixed;
  bottom: 0;
  transition: bottom 0.2s ease-in-out;
  width: 100%;
}

.nav-up {
  bottom: -40px !important;
}

main {
  height: 2000px;
}

footer {
  background: #ddd;
}

div {
  position: fixed;
  bottom:30px;
  right: 5px;
  background:#f5b335;
  border: 2px solid #f5b335;
  border-radius: 5px;
  outline: 0;
  transition: bottom 0.2s ease-in-out;
  cursor: pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<header class="nav-down">
  This is your menu.
  
</header>
<div> Show Navbar </div>
<main>
  This is your body.
  
</main>

<footer>
  This is your footer.
</footer>
0
Ömürcan Cengiz 27 Oktober 2019, 22:13