HTML广播黑客加上同一页锚(链接)

时间:2017-04-13 00:23:42

标签: html css html5 css3

以下是我的代码,它可以根据需要运行。然而,为了使其工作,我必须向onclick元素添加label个事件,有没有办法在不使用onclick代码的情况下同时启动触发和无线电切换的链接?< / p>

没有label元素没关系,但如果没有它们我就无法切换radios

我的期望结果是针对以下代码在3 onclick元素中的内联.magic-label事件中具有相同功能而不使用javascript。所以没有javascript来完成这项工作,如果解决方案需要,我只需要label元素。

感谢您提供任何可能的帮助或解决方案。

html,
body {
  width: 100%;
  height: 100%;
  position: relative;
}

body {
  overflow: hidden;
}

header {
  background: #fff;
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 3.5rem;
  z-index: 10;
}

nav {
  width: 100%;
  padding-top: 0.5rem;
}

nav ul {
  list-style: none;
  width: inherit;
  margin: 0;
}

ul li:nth-child( 3n + 1),
#main .panel:nth-child( 3n + 1) {
  background: rgb( 0, 180, 255);
}

ul li:nth-child( 3n + 2),
#main .panel:nth-child( 3n + 2) {
  background: rgb( 255, 65, 180);
}

ul li:nth-child( 3n + 3),
#main .panel:nth-child( 3n + 3) {
  background: rgb( 0, 255, 180);
}

ul li {
  display: inline-block;
  margin: 0 0px;
  margin: 0 0rem;
  padding: 0px 0px;
  padding: 0.3rem 0.5rem;
  border-radius: 2px;
  line-height: 1.5;
}

ul li a,
.magic-label {
  color: #fff;
  text-decoration: none;
}

.panel {
  width: 100%;
  height: 700px;
  z-index: 0;
  -webkit-transform: translateZ( 0);
  transform: translateZ( 0);
  -webkit-transition: -webkit-transform 0.6s ease-in-out;
  transition: transform 0.6s ease-in-out;
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
}

.panel h1 {
  font-family: sans-serif;
  font-size: 64px;
  font-size: 4rem;
  color: #fff;
  position: relative;
  line-height: 200px;
  top: 20%;
  text-align: center;
  margin: 0;
}


/*
 *Scrolling
 */

a[ id="servicios"]:target ~ #main article.panel {
  -webkit-transform: translateY( 0px);
  transform: translateY( 0px);
}

a[ id="galeria"]:target ~ #main article.panel {
  -webkit-transform: translateY( -500px);
  transform: translateY( -700px);
}

a[ id="contacto"]:target ~ #main article.panel {
  -webkit-transform: translateY( -1000px);
  transform: translateY( -1400px);
}


/* special code for toggling links*/

a.clicked {
  cursor: pointer;
  color: inherit;
}

.radio-button {
  display: none;
}

#serv-radio:checked~#magic .serv,
#gale-radio:checked~#magic .gale,
#cont-radio:checked~#magic .cont {
  color: red;
}
<input type="radio" id="serv-radio" class="radio-button" name="content-magic" checked="checked" />
<input type="radio" id="gale-radio" class="radio-button" name="content-magic" value="" />
<input type="radio" id="cont-radio" class="radio-button" name="content-magic" value="" />

<a id="servicios"></a>
<a id="galeria"></a>
<a id="contacto"></a>

<header id="magic" class="nav">
  <nav>
    <ul>
      <li>
        <label for="serv-radio" class="magic-label serv" onclick="document.getElementById('serv-radio').checked='checked'">
          <a class="clicked" href="#servicios">
            Servicios
          </a>
        </label>
      </li>
      <li>
        <label for="gale-radio" class="magic-label gale" onclick="document.getElementById('gale-radio').checked='checked'">
          <a class="clicked" href="#galeria" >
            Galeria
          </a>
        </label>
      </li>
      <li>
        <label for="cont-radio" class="magic-label cont" onclick="document.getElementById('cont-radio').checked='checked'">
          <a class="clicked" href="#contacto">
            Contacta nos
          </a>
        </label>
      </li>
    </ul>
  </nav>
</header>

<section id="main">
  <article class="panel" id="servicios">
    <h1> Nuestros Servicios</h1>
  </article>

  <article class="panel" id="galeria">
    <h1> Mustra de nuestro trabajos</h1>
  </article>

  <article class="panel" id="contacto">
    <h1> Pongamonos en contacto</h1>
  </article>
</section>

2 个答案:

答案 0 :(得分:2)

我很确定,因为您已经在使用哈希片段来跟踪状态,所以您不需要使用复选框hack来进行设置。

这是您的设置,删除了所有单选按钮和标签:

html,
body {
width: 100%;
height: 100%;
position: relative;
}

body {
overflow: hidden;
}

header {
background: #fff;
position: fixed;
left: 0;
top: 0;
width: 100%;
height: 3.5rem;
z-index: 10;
}

nav {
width: 100%;
padding-top: 0.5rem;
}

nav ul {
list-style: none;
width: inherit;
margin: 0;
}

ul li:nth-child(3n - 2),
#main .panel:nth-child(3n - 2) {
background-color: rgb( 0, 180, 255);
}

ul li:nth-child(3n - 1),
#main .panel:nth-child(3n - 1) {
background-color: rgb( 255, 65, 180);
}

ul li:nth-child(3n),
#main .panel:nth-child(3n) {
background-color: rgb( 0, 255, 180);
}

ul li {
  display: inline-block;
  margin: 0;
  padding: 0;
  border-radius: 2px;
  line-height: 1.5;
}

ul li a {
display: inline-block;
padding: 0.3rem 0.5rem;
color: #fff;
text-decoration: none;
}

.panel {
  width: 100%;
  height: 700px;
  z-index: 0;
  transform: translateZ( 0);
  transition: transform 0.6s ease-in-out;
  backface-visibility: hidden;
}

.panel h1 {
  font-family: sans-serif;
  font-size: 64px;
  font-size: 4rem;
  color: #fff;
  position: relative;
  line-height: 200px;
  top: 20%;
  text-align: center;
  margin: 0;
}


/* Scrolling */
a[id="servicios"]:target ~ #main article.panel {transform: translateY( 0px);}
a[id="galeria"]:target ~ #main article.panel {transform: translateY( -700px);}
a[id="contacto"]:target ~ #main article.panel {transform: translateY( -1400px);}


/* Nav Item Text Color */
header [href="#servicios"],
a[id="galeria"]:target ~ header [href="#galeria"],
a[id="contacto"]:target ~ header [href="#contacto"] {color: rgb(255, 0, 0);}

a[id="galeria"]:target ~ header [href="#servicios"],
a[id="contacto"]:target ~ header [href="#servicios"]
{color: rgb(255, 255, 255);}
<a id="servicios"></a>
<a id="galeria"></a>
<a id="contacto"></a>

<header>
<nav>
<ul>
<li><a href="#servicios">Servicios</a></li>
<li><a href="#galeria" >Galeria</a></li>
<li><a href="#contacto">Contacta nos</a></li>
</ul>
</nav>
</header>

<section id="main">
<article class="panel">
<h1> Nuestros Servicios</h1>
</article>

<article class="panel">
<h1> Mustra de nuestro trabajos</h1>
</article>

<article class="panel">
<h1> Pongamonos en contacto</h1>
</article>
</section>

答案 1 :(得分:0)

您可以尝试添加以下Javascript

$(".magic-label").click(function () {
    var label_id = $(this).attr('for'); //gets the *for* attrbute from the label
    document.getElementById(label_id).checked='checked';
});

XML Namespaces Explained