简体   繁体   English

Skrollr-是否可以使用Sprite并更改滚动位置?

[英]Skrollr - is it possible to use a sprite and change position on scroll?

I have a background image that has 4 images of a character in different walking positions. 我有一个背景图像,其中有4个角色在不同步行位置的图像。

Is it possible on scroll with Skrollr to move the sprite position (let's say by 150px on the x co-ordinate) every 0.5 seconds? 使用Skrollr滚动时是否可以每0.5秒移动精灵位置(假设在x坐标上移动150px)? Want to give it a walking effect but only on scroll. 想要给它一个步行的效果,但是只能在滚动上。

Maybe there's another way to achieve this effect? 也许还有另一种方法可以达到这种效果? Quite keen on only using Skrollr as I've used it in the past and LOVE it! 非常热衷于仅使用Skrollr,因为我过去曾经使用过并且喜欢它!

The only way I've managed to achieve this so far is by creating a sprite of images on a completely landscape canvas type so that all of the images are changed upon the X-Axis then using skrollr stylesheets ( click here for github ) to apply the animation upon scroll. 到目前为止,我设法实现这一目标的唯一方法是在完全横向的画布类型上创建图像精灵,以便在X轴上更改所有图像,然后使用skrollr样式表( 单击此处查看github )进行应用滚动动画。

Start by making a Skrollr.css file. 首先制作一个Skrollr.css文件。 Be sure to include the 'data-skrollr-stylesheet' to the link tag. 确保在链接标记中包含“数据滚动器样式表”。

<link href="css/skrollr.css" rel="stylesheet" data-skrollr-stylesheet>

Right before the closing body tag, include the skrollr stylesheet min js file followed by the skrollr JS. 在body结束标记之前,先添加skrollr样式表min js文件,再添加skrollr JS。 See below. 见下文。

<script type = "text/javascript" src="dist/skrollr.stylesheets.min.js"></script>
<script type = "text/javascript" src="js/skrollr.js"></script>

To get you started I've placed a simple demo of what your skrollr stylesheet should look like. 为了让您入门,我已经简单地演示了skrollr样式表的外观。

.div-name {
    -skrollr-animation-name: animation1;
}

@-skrollr-keyframes animation1 {
    500 {
        background-position: 0px;
    }

    600 {
        background-position: -100px;
    }
}

The numbers in the animation are relative to the top of the browser window. 动画中的数字是相对于浏览器窗口顶部的。 600px from the top of the window, the background-position will move -100px on the X-axis. 从窗口顶部开始600px,背景位置将在X轴上移动-100px。

I am currently trying to figure out how to remove the transition/slider effect on scroll. 我目前正在尝试找出如何删除滚动上的过渡/滑块效果。 Personally I'd like the images to change on demand rather than scroll. 我个人希望图像按需更改而不是滚动。 I hope my instruction has given you something to start with and if I figure out how to change the image instantly without the transition, I will expand on this here. 希望我的指导给您一些开始,如果我想出了如何在不进行过渡的情况下立即更改图像的方法,那么我将在此处进行扩展。 Good luck with the project. 祝项目顺利。

I managed to include frame-by-frame sprite animation in skrollr by adding a custom easing that does the trick. 我设法通过添加自定义缓动来在skrollr中包括逐帧精灵动画。 Math.round in my intuition would return sort of a square wave and surprisingly it worked (but be aware, I'm not sure about the math behind it): 根据我的直觉,Math.round将返回某种方波,并且令人惊讶地它起作用了(但是请注意,我不确定其背后的数学原理):

var s = skrollr.init({ 
    easing: {
        frames: function(p) {
            return Math.round(Math.sin(p) * 50);
        }
    }
});

This custom easing will: loop your animation without any interpolation so that you don't get that infamous "sliding" instead of the animation. 此自定义缓动将:在没有任何插值的情况下循环播放动画,以免获得臭名昭著的“滑动”而不是动画。

After that you call the animation on css this way (please note I'm using skrollr stylesheet but there's no reason why it shouldn't work directly with html data tags as well): 之后,您可以通过这种方式在CSS上调用动画(请注意,我使用的是skrollr样式表,但没有理由为什么它也不能直接与html数据标签一起使用):

.animation{
    display: block;
    width: 208px; /*your sprite width*/
    height: 227px; /*you sprite height*/
    background-image: url(yourspritesheet.png);
    background-position: 0px 0px;
    -skrollr-animation-name: youranimation;
}

@-skrollr-keyframes youranimation{
    start{background-position-x[frames]:0px;}
    end{background-position-x[frames]:208px;}
}

In this example, the spritesheet is horizontal and 208px is the sprite width. 在此示例中,spritesheet是水平的,208px是sprite的宽度。 The code will go through the spritesheet at steps of 208px and start from the beginning when it reaches the end. 该代码将以208px的步长通过spritesheet,并在到达末尾时从头开始。 Tested on Chrome, Firefox and Safari. 在Chrome,Firefox和Safari上进行了测试。 You can find an example of the result here (school project): http://www.sartirani.it/daniele/scroll/public_html/ 您可以在此处(学校项目)找到结果的示例: http : //www.sartirani.it/daniele/scroll/public_html/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM