如何创建将由click触发的动画

时间:2015-08-24 19:43:37

标签: css

如何实现以下效果:当用户点击链接时,他会看到当前页面的内容慢慢向上移动,然后显示下一页的新内容。如果可能,我宁愿只使用CSS。

2 个答案:

答案 0 :(得分:2)

可以通过转换实现仅CSS的解决方案。这是 the example ,代码如下:

CSS

.message {
    background:#181818;
    color:#FFF;
    position: absolute;
    top: -250px;
    left: 0;
    width: 100%;
    height: 250px;
    padding: 20px;
    transition: top 300ms cubic-bezier(0.17, 0.04, 0.03, 0.94);
    overflow: hidden;
    box-sizing: border-box;

    }

.message h1 {
  color:#FFF;
}

#toggle {
  position:absolute;
  appearance:none;
  cursor:pointer;
  left:-100%;
  top:-100%;
}

#toggle + label {
  position:absolute;
  cursor:pointer;
  padding:10px;
  background: #26ae90;
width: 100px;
border-radius: 3px;
padding: 8px 10px;
color: #FFF;
line-height:20px;
font-size:12px;
text-align:center;
-webkit-font-smoothing: antialiased;
cursor: pointer;
  margin:20px 50px;
  transition:all 500ms ease;
}
#toggle + label:after {
  content:"Open" 
}

.container {
transition: margin 300ms cubic-bezier(0.17, 0.04, 0.03, 0.94);
  padding:5em 3em;
}

#toggle:checked ~ .message {
  top: 0;
}

#toggle:checked ~ .container {
  margin-top: 250px;
}

#toggle:checked + label {
  background:#dd6149;
}

#toggle:checked + label:after {
  content:"Close"
}

<强> HTML

<input type="checkbox" name="toggle" id="toggle" />    
<label for="toggle"></label>      
<div class="container">
  <h1> Click the Open button to see hidden mesage.</h1>    
</div>
<div class="message">
  <h1> hello, I'm a hidden message. You found it.</h1>
</div>

答案 1 :(得分:1)

以下是一种方法:

HTML 将您的所有网页都包含在<main> div中(或其他)

<body>
    <main>
        <a href="http://thevolumeproject.com">some link</a>
    </main>
</body>

CSS 使<main>处于固定位置,窗口大小

main {
    position: fixed; top: 0; left: 0;
    width: 100%; height: 100%;
}

<强>的jQuery

$('a').click(function(event){ // when click on any link <a>
    event.preventDefault(); // prevents <a> natural behavior (sending you somewhere)
    var URL = $(this).attr('href'); // stores the <a> href attribute
    $('main').animate({top:'-=100%'},1000,function(){ // moves up <main> and when it's gone…
        window.location.href = URL; // sends you where you belong
    });
});

干杯!

&#13;
&#13;
$('a').click(function(event){
	event.preventDefault();
    var URL = $(this).attr('href');
	$('main').animate({top:'-=100%'},1000,function(){
        window.location.href = URL;
    });
});
&#13;
main {
    position: fixed; top: 0; left: 0;
    width: 100%; height: 100%;
    background-color: #DDD;
    padding: 10px;
    box-sizing: border-box;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<main>
    <a href="http://thevolumeproject.com/">some link</a>
</main>
&#13;
&#13;
&#13;