我正在尝试根据用户滚动条在面板内的元素上创建自定义的淡入/淡出效果。 -
所以从0-300开始,我希望该项根据滚动的值淡入-因此在pos 0-不透明度为0-但到300时,不透明度为1。
https://jsfiddle.net/26jkLnup/1/
我认为我已经设法适应了代码库-但感觉不稳定或干净-我走在正确的轨道上吗?
-子画面从左上方开始-但随后跳到右侧。 -我认为有时只有在用户采取某些措施后,精灵才会显示出来-该代码中是否存在错误?
let data = [{
"structure": {
"name": "square",
"height": 30,
"width": 30,
"x": 0,
"y": 0,
"background": 'url("https://i.pinimg.com/originals/74/f3/5d/74f35d5885e8eb858e6af6b5a7844379.jpg")'
},
"frames": [{
"animation": "fadein",
"start": 0,
"stop": 300,
"startPositionX": 0,
"startPositionY": 0,
"endPositionX": 90,
"endPositionY": 0,
}, {
"animation": "move",
"start": 301,
"stop": 600,
"startPositionX": 90,
"startPositionY": 0,
"endPositionX": 90,
"endPositionY": 80,
}, {
"animation": "fadeout",
"start": 601,
"stop": 900,
"positionX": 0,
"positionY": 0,
}],
}/*,
{
"structure": {
"name": "pear",
"height": 30,
"width": 30,
"x": 90,
"y": 80,
"background": 'url("https://image.flaticon.com/icons/svg/272/272135.svg")'
},
"frames": [{
"animation": "move",
"start": 0,
"stop": 300,
"startPositionX": 90,
"startPositionY": 80,
"endPositionX": 0,
"endPositionY": 80,
}, {
"animation": "move",
"start": 301,
"stop": 600,
"startPositionX": 0,
"startPositionY": 80,
"endPositionX": 0,
"endPositionY": 0,
}, {
"animation": "move",
"start": 601,
"stop": 900,
"startPositionX": 0,
"startPositionY": 0,
"endPositionX": 90,
"endPositionY": 80,
}, {
"animation": "show",
"start": 601,
"stop": 9999,
"positionX": 90,
"positionY": 80,
}],
}*/
]
let animations = {
setup: function($container) {
this.$container = $container;
this.viewportWidth = $container.width();
this.viewportHeight = $container.height();
},
createBlock: function(blockSpec) {
let $block = $('<div>');
$block.addClass(blockSpec.name);
$block.addClass("animatedblock");
$block.css("height", blockSpec.height);
$block.css("width", blockSpec.width);
$block.css("background", blockSpec.background);
$block.css("background-size", "cover");
this.$container.append($block);
this.setPosition($block, blockSpec.x, blockSpec.y)
return $block;
},
setPosition($block, x, y) {
$block.css({
left: x / 100 * this.viewportWidth,
top: y / 100 * this.viewportHeight,
});
},
moveBlock($block, frame, scrollProgress) {
let blockPositionX = frame.startPositionX + scrollProgress * (frame.endPositionX - frame.startPositionX);
let blockPositionY = frame.startPositionY + scrollProgress * (frame.endPositionY - frame.startPositionY);
this.setPosition($block, blockPositionX, blockPositionY);
},
showBlock: function($block, frame) {
$block.show()
this.setPosition($block, frame.positionX, frame.positionY);
},
hideBlock: function($block) {
$block.hide()
},
fadeinBlock: function($block, frame, scrollProgress) {
console.log("scrollProgress", scrollProgress)
$block.css({
opacity: 1 * scrollProgress
})
/*
$block.css({
opacity: frame.startPositionY / 100 * this.viewportHeight
})*/
},
fadeoutBlock: function($block, frame, scrollProgress) {
console.log("scrollProgress22222",scrollProgress)
/*
$block.css({
opacity: frame.startPositionY / 100 * this.viewportHeight
})*/
$block.css({
opacity: 1 * (1-scrollProgress)
})
},
}
class ScrollObserver {
constructor() {
let $window = $(window);
this.STOP_DISPATCH = 'STOP_DISPATCH';
this.subscribers = [];
$window.scroll(event => this.dispatch($window.scrollTop()));
}
subscribe(subscriberFn) {
this.subscribers.push(subscriberFn);
}
dispatch(scrollPosition) {
for (let subscriberFn of this.subscribers) {
if (subscriberFn(scrollPosition) == this.STOP_DISPATCH) break;
}
}
}
jQuery(function($) {
animations.setup($('.animationcontainer'));
$(window).resize(event => animations.setup($('.animationcontainer')));
for (let obj of data) {
let scrollObserver = new ScrollObserver();
let blockSpec = obj.structure;
let $block = animations.createBlock(blockSpec);
for (let frame of obj.frames) {
scrollObserver.subscribe(scrollPosition => {
if (scrollPosition >= frame.start && scrollPosition <= frame.stop) {
let scrollProgress = (scrollPosition - frame.start) / (frame.stop - frame.start);
switch (frame.animation) {
case 'move':
animations.moveBlock($block, frame, scrollProgress);
break;
case 'show':
animations.showBlock($block, frame);
break;
case 'fadein':
animations.fadeinBlock($block, frame, scrollProgress);
break;
case 'fadeout':
animations.fadeoutBlock($block, frame, scrollProgress);
break;
}
return scrollObserver.STOP_DISPATCH;
}
});
}
}
});