微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

html5 – 使用Transition / CSS3滚动到锚点

我有一系列使用锚机制的链接
<div class="header">
    <p class="menu"><a href="#S1">Section1</a></p>
    <p class="menu"><a href="#S2">Section2</a></p>
    ...
</div>
<div style="width: 100%;">
<a name="S1" class="test">&nbsp;</a>
<div class="curtain">
Lots of text
</div>

<a name="S2" class="test">&nbsp;</a>
<div class="curtain">
lots of text
</div>
...
</div>

我正在使用以下CSS:

.test
{
    position:relative; 
    margin: 0; 
    padding: 0; 
    float: left;
    display: inline-block;
    margin-top: -100px; /* whatever offset this needs to be */
}

工作正常当然,当我们点击链接时,它从一个部分跳转到另一个部分.所以我想要顺利过渡,使用某种滚动来选择开始
部分.

我想我在Stackoverflow上看到这是不可能的(但是)与CSS3,但我想要一个确认,我也想知道什么是可能的解决方案.我很高兴使用JS,但我不能使用jQuery.我尝试在链接上使用点击功能,检索需要显示的div的“垂直位置”,但是我没有成功.我还在学习JS,不太了解,想出了我自己的解决方案.

任何帮助/想法将不胜感激.

解决方法

您可以在以下页面找到您的问题的答案:

http://stackoverflow.com/a/17633941/2359161

这是给出的JSfiddle

http://jsfiddle.net/YYPKM/3/

HTML:

<a id="servicios"></a>
<a id="galeria"></a>
<a id="contacto"></a>
<header class="nav">
    <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" 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>

CSS(注意结尾的滚动部分,具体):

/*
 *Styling
 */

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 8px;
    margin: 0 0.5rem;
    padding: 5px 8px;
    padding: 0.3rem 0.5rem;
    border-radius: 2px; 
    line-height: 1.5;
}

ul li a {
    color: #fff;
    text-decoration: none;
}

.panel {
    width: 100%;
    height: 500px;
    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: 33%;
    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( -500px );
}
a[ id= "contacto" ]:target ~ #main article.panel {
    -webkit-transform: translateY( -1000px );
    transform: translateY( -1000px );
}

原文地址:https://www.jb51.cc/html5/168757.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。