自定义Bootstrap导航栏,带有居中徽标

时间:2018-05-22 11:22:27

标签: javascript html css bootstrap-4

我正在尝试使用居中徽标和阴影效果重新创建此fancy nav

我已经设法让JS效果在图像缩小滚动时工作。

我有这么远,但如何定位图像使其在导航器中心?

另外,如何在导航栏和图像下创建阴影?

HTML

    <nav class="navbar navbar-expand-lg sticky-top navbar-light bg-light 
    flex-column">
  <div class="container">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>
<div class="navbar-collapse collapse" id="navbarNav">
  <ul class="navbar-nav nav-fill w-100">
    <li class="nav-item active align-self-center">
      <a class="nav-link" href="#">About<span class="sr-only">(current)</span></a>
    </li>
    <li class="nav-item align-self-center">
      <a class="nav-link" href="#">Books</a>
    </li>
    <li class="nav-item">
      <a class="navbar-brand mx-auto" href="index.html"> <img src="https://placeimg.com/640/480/any" class="largeLogo" id="Logo" alt="Star Books"></a>
    </li>
    <li class="nav-item align-self-center">
      <a class="nav-link" href="#">Book Club</a>
    </li>
    <li class="nav-item align-self-center">
      <a class="nav-link" href="#">Contact</a>
    </li>
  </ul>
   </div>
 </div>
</nav>

CSS

#Logo {
  position: absolute;
  transition: all 0.5s;
  border-radius: 50%;
}

 .largeLogo {
   width: 200px;
}

 .smallLogo {
   width: 100px;
}

JS

window.onscroll = function() {
  growShrinkLogo()
};

var Logo = document.getElementById("Logo");
var endOfDocumentTop = 150;
var size = 0;

function growShrinkLogo() {
  var scroll = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;

  if (size == 0 && scroll > endOfDocumentTop) {
    Logo.className = 'smallLogo';
    size = 1;
  } else if (size == 1 && scroll <= endOfDocumentTop) {
    Logo.className = 'largeLogo';
    size = 0;
  }
}

JSFiddle

1 个答案:

答案 0 :(得分:0)

您需要添加right: 0; left: 0; margin: 0 auto;以将图像对齐到中心位置。此外,使用box-shadow属性为导航栏和图像添加阴影。

&#13;
&#13;
window.onscroll = function() {
  growShrinkLogo()
};

var Logo = document.getElementById("Logo");
var endOfDocumentTop = 150;
var size = 0;

function growShrinkLogo() {
  var scroll = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;

  if (size == 0 && scroll > endOfDocumentTop) {
    Logo.className = 'smallLogo';
    size = 1;
  } else if (size == 1 && scroll <= endOfDocumentTop) {
    Logo.className = 'largeLogo';
    size = 0;
  }
}
&#13;
body {
  height: 2000px;
}

#Logo {
  position: absolute;
  right: 0;
  left: 0;
  margin: 0 auto;
  transition: all 0.5s;
  border-radius: 50%;
  box-shadow: 0px 5px 4px rgba(0, 0, 0, 0.3);
}

.largeLogo {
  width: 200px;
}

.smallLogo {
  width: 100px;
}

nav {
  box-shadow: 0px 3px 1px rgba(0, 0, 0, 0.1);
}
&#13;
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet"/>

<!-- 
  Bootstrap docs: https://getbootstrap.com/docs
-->

<nav class="navbar navbar-expand-lg sticky-top navbar-light bg-light flex-column">
  <div class="container">
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
				<span class="navbar-toggler-icon"></span>
			</button>
    <div class="navbar-collapse collapse" id="navbarNav">
      <ul class="navbar-nav nav-fill w-100">
        <li class="nav-item active align-self-center">
          <a class="nav-link" href="#">About<span class="sr-only">(current)</span></a>
        </li>
        <li class="nav-item align-self-center">
          <a class="nav-link" href="#">Books</a>
        </li>
        <li class="nav-item">
          <a class="navbar-brand mx-auto" href="index.html"> <img src="https://placeimg.com/640/480/any" class="largeLogo" id="Logo" alt="Star Books"></a>
        </li>
        <li class="nav-item align-self-center">
          <a class="nav-link" href="#">Book Club</a>
        </li>
        <li class="nav-item align-self-center">
          <a class="nav-link" href="#">Contact</a>
        </li>
      </ul>
    </div>
  </div>
</nav>
&#13;
&#13;
&#13;