在我的應用程序(Vaadin 7.6.1)中,我想使用平滑滾動進行定位。 是否可以使用Javascript爲此滾動設置動畫效果?Vaadin 7.6.1。 +平滑滾動
您能否指點我正確的方向?
我嘗試這樣做,但它不工作:
DemoUI.java
@Theme("demo")
@JavaScript({"example.js"})
public class DemoUI extends UI {
@Override
protected void init(VaadinRequest request) {
final VerticalLayout vLayout = new VerticalLayout();
vLayout.setMargin(true);
setContent(vLayout);
Button button = new Button("Click Me");
button.addClickListener(e ->
Page.getCurrent().getJavaScript().execute(
"smoothScroll(document.getElementById('anchor'), 2000)");
);
vLayout.addComponent(button);
}
...
VerticalLayout aaa = new VerticalLayout();
aaa.setId("anchor");
vLayout.addComponent(aaa);
...
}
example.js
function smoothScroll(target, time) {
// time when scroll starts
var start = new Date().getTime(),
// set an interval to update scrollTop attribute every 25 ms
timer = setInterval(function() {
// calculate the step, i.e the degree of completion of the smooth scroll
var step = Math.min(1, (new Date().getTime() - start)/time);
// calculate the scroll distance and update the scrollTop
document.body['scrollTop'] = (step * target.offsetTop);
// end interval if the scroll is completed
if (step == 1) clearInterval(timer);
}, 25);
}