实际上,演示文件中的 javascript 很好地暗示了如何轻松完成类似的事情。
我在这里复制了整个 html 和 js,另外两行在标题压缩时隐藏了标题。
之所以我把 128 m 作为 翻译 Y 是因为最大标题高度是 192 d.height,浓缩高度为64 d.condensedHeight。当头部被压缩时,它在 y 轴上移动 128 m (192 - 64 d.height - d.condensedHeight) 的距离。
<body unresolved>
<core-scroll-header-panel condenses keepcondensedheader>
<core-toolbar class="tall">
<core-icon-button icon="arrow-back"></core-icon-button>
<div flex></div>
<core-icon-button icon="search"></core-icon-button>
<core-icon-button icon="more-vert"></core-icon-button>
<div class="bottom indent title">Title</div>
</core-toolbar>
<div class="content">
<sample-content size="100"></sample-content>
</div>
</core-scroll-header-panel>
<script>
// custom transformation: scale header's title
var titleStyle = document.querySelector('.title').style;
// added code - here we need to obtain the title div as well
var title = document.querySelector('.title');
addEventListener('core-header-transform', function (e) {
var d = e.detail;
var m = d.height - d.condensedHeight;
var scale = Math.max(0.75, (m - d.y) / (m / 0.25) + 0.75);
titleStyle.transform = titleStyle.webkitTransform =
'scale(' + scale + ') translateZ(0)';
// added code - here we hide the title when the header is condensed
title.hidden = d.y == m;
});
</script>
</body>
希望这会有所帮助!
更新
实际上,现在我想,为title 的opacity 设置动画而不是显示/隐藏它可能是一个更好的主意。它将提供更好的用户体验以及core-scroll-header-panel 的平滑滚动和褪色背景!
感谢core-animation,它非常简单。
首先我们需要包含这个引用。
<link rel="import" href="../core-animation/core-animation.html">
然后是淡入淡出动画的定义。
<body unresolved>
<!--define the opacity animations-->
<core-animation id="out" fill="forwards" duration="400">
<core-animation-keyframe>
<core-animation-prop name="opacity" value="1"></core-animation-prop>
</core-animation-keyframe>
<core-animation-keyframe>
<core-animation-prop name="opacity" value="0"></core-animation-prop>
</core-animation-keyframe>
</core-animation>
<core-animation id="in" fill="forwards" duration="400">
<core-animation-keyframe>
<core-animation-prop name="opacity" value="0"></core-animation-prop>
</core-animation-keyframe>
<core-animation-keyframe>
<core-animation-prop name="opacity" value="1"></core-animation-prop>
</core-animation-keyframe>
</core-animation>
最后,我们在 core-header-transform 处理程序中调用它们。
// animation variables
var fadeIn, fadeOut;
// create a local bool to ensure the 'fade in' animation is only called once
var condensed = false;
// retrieve the animations and set their targets
addEventListener('polymer-ready', function (e) {
fadeOut = document.getElementById('out');
fadeOut.target = title;
fadeIn = document.getElementById('in');
fadeIn.target = title;
});
addEventListener('core-header-transform', function (e) {
var d = e.detail;
var m = d.height - d.condensedHeight;
var scale = Math.max(0.75, (m - d.y) / (m / 0.25) + 0.75);
titleStyle.transform = titleStyle.webkitTransform =
'scale(' + scale + ') translateZ(0)';
// when the header is condensed, we fade it out
if (d.y == m) {
condensed = true;
fadeOut.play();
}
// otherwise, we fade it back in
else {
if (condensed) {
condensed = false;
fadeIn.play();
}
}
});