菜单指针通过滚动动画(Menu pointer animate by scroll)

编程入门 行业动态 更新时间:2024-10-26 10:37:18
菜单指针通过滚动动画(Menu pointer animate by scroll)

我做了一个顶级菜单,按钮上有一个小箭头。 我想让这个箭头移动到我的单页设计上滚动的位置。

我认为我已经掌握了基础知识......但是一旦我尝试为箭头设置动画(而不是让它在按钮之间跳转),它就会不知所措。 我想这与倍数> = ...有关但我想不出另类。 有人可以给我一些建议吗?

效果可以在这里看到: http : //www.lightwavedesign.nl/

我正在使用的JS如下:

function goToByScroll(id){ $('html,body').animate({scrollTop: $("#"+id).offset().top-78},'1000'); }; window.onload = function() { var link1 = $("#Welkom"); var positionLink1 = link1.position(); var link2 = $("#OverMij"); var positionLink2 = link2.position(); var link3 = $("#Portfolio"); var positionLink3 = link3.position(); var link4 = $("#Contact"); var positionLink4 = link4.position(); function getScrollTop() { if (typeof window.pageYOffset !== 'undefined' ) {return window.pageYOffset;} var d = document.documentElement; if (d.clientHeight) {return d.scrollTop;} return document.body.scrollTop; } window.onscroll = function() { var box = document.getElementById('menuAanwijzer'), scroll = getScrollTop(); if (scroll <= positionLink2.top) {$('#menuAanwijzer').animate({left: '665px'}, 100)} if (scroll >= positionLink2.top-80) {$('#menuAanwijzer').animate({left: '760px'}, 100)} if (scroll >= positionLink3.top-80) {$('#menuAanwijzer').animate({left: '860px'}, 100)} if (scroll >= positionLink4.top-80) {$('#menuAanwijzer').animate({left: '960px'}, 100)} if (scroll >= positionLink2.top-80) {$('#menuTitel').fadeIn('500');} if (scroll <= positionLink2.top-80) {$('#menuTitel').fadeOut('500');} }; };

I have made an top menu with underneith the buttons a little arrow. I want this arrow to move correspoing to where the scroll is on my one page design.

I think i got the basics working... but somehow it keeps flippin around as soon as i try to animate the arrow (instead of making it jump between buttons). I guess it has something to do with the multiple >=... but i can't think of an alternative. Can anyone please give me some advice?

The effect can be seen here: http://www.lightwavedesign.nl/

The JS i am using is as follow:

function goToByScroll(id){ $('html,body').animate({scrollTop: $("#"+id).offset().top-78},'1000'); }; window.onload = function() { var link1 = $("#Welkom"); var positionLink1 = link1.position(); var link2 = $("#OverMij"); var positionLink2 = link2.position(); var link3 = $("#Portfolio"); var positionLink3 = link3.position(); var link4 = $("#Contact"); var positionLink4 = link4.position(); function getScrollTop() { if (typeof window.pageYOffset !== 'undefined' ) {return window.pageYOffset;} var d = document.documentElement; if (d.clientHeight) {return d.scrollTop;} return document.body.scrollTop; } window.onscroll = function() { var box = document.getElementById('menuAanwijzer'), scroll = getScrollTop(); if (scroll <= positionLink2.top) {$('#menuAanwijzer').animate({left: '665px'}, 100)} if (scroll >= positionLink2.top-80) {$('#menuAanwijzer').animate({left: '760px'}, 100)} if (scroll >= positionLink3.top-80) {$('#menuAanwijzer').animate({left: '860px'}, 100)} if (scroll >= positionLink4.top-80) {$('#menuAanwijzer').animate({left: '960px'}, 100)} if (scroll >= positionLink2.top-80) {$('#menuTitel').fadeIn('500');} if (scroll <= positionLink2.top-80) {$('#menuTitel').fadeOut('500');} }; };

最满意答案

使用.stop()简单清除动画队列 .stop()。动画()

if (scroll <= positionLink2.top) {$('#menuAanwijzer').stop().animate({left: '665px'}, 100)} if (scroll >= positionLink2.top-80) {$('#menuAanwijzer').stop().animate({left: '760px'}, 100)} if (scroll >= positionLink3.top-80) {$('#menuAanwijzer').stop().animate({left: '860px'}, 100)} if (scroll >= positionLink4.top-80) {$('#menuAanwijzer').stop().animate({left: '960px'}, 100)} if (scroll >= positionLink2.top-80) {$('#menuTitel').stop().fadeIn('500');} if (scroll <= positionLink2.top-80) {$('#menuTitel').stop().fadeOut('500');}

Simple, clear your animation queue using .stop() .stop().animate()

if (scroll <= positionLink2.top) {$('#menuAanwijzer').stop().animate({left: '665px'}, 100)} if (scroll >= positionLink2.top-80) {$('#menuAanwijzer').stop().animate({left: '760px'}, 100)} if (scroll >= positionLink3.top-80) {$('#menuAanwijzer').stop().animate({left: '860px'}, 100)} if (scroll >= positionLink4.top-80) {$('#menuAanwijzer').stop().animate({left: '960px'}, 100)} if (scroll >= positionLink2.top-80) {$('#menuTitel').stop().fadeIn('500');} if (scroll <= positionLink2.top-80) {$('#menuTitel').stop().fadeOut('500');}

更多推荐

本文发布于:2023-07-17 13:19:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1145016.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:指针   菜单   动画   Menu   animate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!