如果onclick激活添加类

时间:2018-09-29 09:38:17

标签: javascript jquery

我有一个菜单。我将功能滚动到菜单中列出的项目。我还希望所选元素显示hr。如果没有滚动功能,我会看到hr出现在所选菜单中。当菜单上单击并显示小时和滚动功能时,我需要它。让我们看一下链接2和链接3

	$('a.scrollTo').on('click', function(){

  // data-scrollTo = section scrolling to name
  var scrollTo = $(this).attr('data-scrollTo');
  
  
  // toggle active class on and off. added 1/24/17
  $( "a.scrollTo" ).each(function() {
    if(scrollTo == $(this).attr('data-scrollTo')){
      $(this).addClass('active');    
    }else{
      $(this).removeClass('active');
    }
  });
  
  
  // animate and scroll to the sectin 
  $('body, html').animate({
    
    // the magic - scroll to section
    "scrollTop": $('#'+scrollTo).offset().top
  }, 1000 );
  return false;
  
})


  var elems = document.querySelectorAll(".one,.two,.three");
for (var i = 0; i < elems.length; i++) {
  elems[i].addEventListener("click",function(e){
    if(document.querySelector(".activeOne")){
      document.querySelector(".activeOne").classList.remove("activeOne");
    }
    e.currentTarget.classList.add("activeOne");
  });
} 
* {
    margin: 0;
    padding: 0;
    outline: none;
}
body, html {
    width: 100%;
    height: 100%;
}
#main-header {
    background-image: url(../img/background.jpg);
    min-height: 100%;max-width: 100%;
    background-attachment: fixed;
    background-repeat: repeat-x;
    background-position: left center;
    position: relative;}	
    .main-overlay {
        position: absolute;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
        opacity: .5;
        z-index: 0;
        background-image: linear-gradient(to right, #34373a, #696c70);
        background-image: -moz-linear-gradient(to right, #34373a, #696c70);
        background-image: -webkit-linear-gradient(to left, #34373a, #696c70);
        background-image: -ms-linear-gradient(to left, #34373a, #696c70);
    }
        .header {
        position: relative;
        z-index: 1;
    }
    #headet-title {
        text-align: center;
        color: white;
        font-family: 'Cabin Condensed', sans-serif;
        margin: 15% 0 0.5% 0;
font-size: 1.7em;
    }
    #description-header {
        text-align: center;
        color: white;
        font-family: 'Cabin Condensed', sans-serif;
        font-size: 1.4em;
        width: 50%;
        margin: auto;
}
#menu-nav a {
    color: white;
      text-decoration:none;
  color:#fff;
  width:120px;
  display:inline-block;
  text-align:center;
  transition:all .33s linear;
  -webkit-transition:all .33s linear;
}

ul li {
  display: inline;
  text-align: center;
}

.two:hover ~ hr {
  margin-left: 44%;
}

.three:hover ~ hr {
  margin-left: 78%;
}

.scrollTo.activeOne ~ hr {
  margin-left: 10%;
}

.two.activeOne ~ hr {
  margin-left: 44%;
}

.three.activeOne ~ hr {
  margin-left: 78%;
}




hr {
  height: .15rem;
  width: 12%;
  margin: 0;
  background: #ff2a58;
  border: none;
  transition: .3s ease-in-out;
  margin-left: 10%;
}
.menu-nav-header {
display: flex;
justify-content: space-between;
padding: 5px;
}
#logo h2 {
color: white;
        font-family: 'Cabin Condensed', sans-serif;
}
.button {
    text-align: center;
    margin-top: 2%;
}
.button a {
    color: white;
    font-family: 'Cabin Condensed', sans-serif;
    text-decoration: none;
    border: 1px solid;
padding: 6px 12px 6px 12px;
border-radius: 3px;
font-size: 1.3em;
}
.header-title-border {
height: 2px;
width: 190px;
background-color: #ff2a58;
margin: auto;
margin-bottom: 1%;
}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <link href="scroll/section-scroll.css" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="scroll/jquery.section-scroll.js"></script>
<link rel="stylesheet" href="css/style.css" type="text/css" charset="utf-8"/>
<link href="https://fonts.googleapis.com/css?family=PT+Sans+Narrow:400,700" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Cabin+Condensed" rel="stylesheet"> 
</head>
<div id="main-header">
<div class="main-overlay"></div> 
  <div class="header"> 
    <div class="menu-nav-header">    <div id="logo">
      <h2>NAME</h2>
    </div>
    <div id="menu-nav">
  <ul>
    <li class="one"><a class="scrollTo" data-scrollTo="main-header" href="#">Link 1</a></li>
<li class="two"> <a class="scrollTo" data-scrollTo="content1" href="#">Link 2</a></li>
<li class="three"> <a class="scrollTo" data-scrollTo="content2" href="#">Link 3</a></li>

    <hr />
  </ul>
  </div>
</div>
    <div id="headet-title">
      <h1 class="wow fadeInDown">Name of Your Company</h1>
    </div>
    <div class="header-title-border"></div>
    <div id="description-header">
    <span class="wow fadeInDown">Lorem ipsum dolor sit amet consectetur adipisicing elit. Atque voluptate commodi impedit, 
        vitae vero, quo et odit natus sequi odio reprehenderit expedita voluptatum aspernatur, dolore soluta corporis aliquid animi iure.</span>
</div>
<div class="button">
  <a href="#" class="button-a">Get Started</a>
</div>

</div>
</div> 
<div id="content1">Hello</div>

链接2具有content1,这就是为什么hr不出现而链接3没有content2并且hr出现的原因。请帮助我

0 个答案:

没有答案