René's URL Explorer Experiment


Title: 【JavaScript】【学习心得】学习 JavaScript 第九天 · Issue #13 · paddingme/Learning-JavaScript · GitHub

Open Graph Title: 【JavaScript】【学习心得】学习 JavaScript 第九天 · Issue #13 · paddingme/Learning-JavaScript

X Title: 【JavaScript】【学习心得】学习 JavaScript 第九天 · Issue #13 · paddingme/Learning-JavaScript

Description: 用 JavaScript 实现动画效果 动画的基础知识 如前文所说,如果我们想随着时间的变化而不断改变某个元素的样式,目前 CSS 尚且无能为力,只能使用 JavaScript。JavaScript 能够按照预定的时间间隔重复调用一个函数,而这意味着我们可以随着世家的推移而不断改变某个某个元素的样式。 动画是样式随时间变化的完美例子之一,简单的说,动画就是让元素的位置随着时间而不断的发生变化。

Whee!

function pos...

Open Graph Description: 用 JavaScript 实现动画效果 动画的基础知识 如前文所说,如果我们想随着时间的变化而不断改变某个元素的样式,目前 CSS 尚且无能为力,只能使用 JavaScript。JavaScript 能够按照预定的时间间隔重复调用一个函数,而这意味着我们可以随着世家的推移而不断改变某个某个元素的样式。 动画是样式随时间变化的完美例子之一,简单的说,动画就是让元素的位置随着时间而不断的发生变化...

X Description: 用 JavaScript 实现动画效果 动画的基础知识 如前文所说,如果我们想随着时间的变化而不断改变某个元素的样式,目前 CSS 尚且无能为力,只能使用 JavaScript。JavaScript 能够按照预定的时间间隔重复调用一个函数,而这意味着我们可以随着世家的推移而不断改变某个某个元素的样式。 动画是样式随时间变化的完美例子之一,简单的说,动画就是让元素的位置随着时间而不断的发生变化...

Opengraph URL: https://github.com/paddingme/Learning-JavaScript/issues/13

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"【JavaScript】【学习心得】学习 JavaScript 第九天","articleBody":"# 用 JavaScript 实现动画效果\n## 动画的基础知识\n\n如前文所说,如果我们想随着时间的变化而不断改变某个元素的样式,目前 CSS 尚且无能为力,只能使用 JavaScript。JavaScript 能够按照预定的时间间隔重复调用一个函数,而这意味着我们可以随着世家的推移而不断改变某个某个元素的样式。\n\n动画是样式随时间变化的完美例子之一,简单的说,动画就是让元素的位置随着时间而不断的发生变化。\n\n``` html\n    \u003cp id=\"message\"\u003eWhee!\u003c/p\u003e\n```\n\n``` js\nfunction positionMessage () {\n  if (!document.getElementById) return false;\n  if (!document.getElementById(\"message\")) return false;\n  var elem = document.getElementById(\"message\");\n  elem.style.position = \"absolute\";\n  elem.style.left = \"50px\";\n  elem.style.top = \"100px\";\n}\n\naddLoadEvent(positionMessage);\n```\n\n加载之后立刻发生变化。\n### 时间\n\n``` js\n    setTimeout(\"function\",interval);// \"function\" 为函数名,interval 为以毫秒为单位的时间\n```\n\n``` js\nfunction moveMessage() {\n    if (!document.getElementById) return false;\n    if (!document.getElementById(\"message\")) return false;\n    var elem = document.getElementById(\"message\");\n    elem.style.left = \"500px\";\n}\n\nfunction positionMessage () {\n  if (!document.getElementById) return false;\n  if (!document.getElementById(\"message\")) return false;\n  var elem = document.getElementById(\"message\");\n  elem.style.position = \"absolute\";\n  elem.style.left = \"250px\";\n  elem.style.top = \"100px\";\n  movement = setTimeout(\"moveMessage()\",5000);\n}\n\naddLoadEvent(positionMessage);\n```\n\n取消某个正在排队等待执行的函数:\n\n``` js\nvar variable = setTimeout(\"function\",interval);\nclearTimeout(variable);\n```\n\n在线 DEMO: http://codepen.io/paddingme/pen/jEPBqg\n## 实用的动画\n\n看完实用的动画一节之后,发现这里的其实和我之前学习的 imooc 的视频教程[回到顶部效果](http://www.imooc.com/learn/65) 是一样的。建议大家观看学习,\n这里记录下我学这节课的笔记,供大家参考:\n\n在浏览网页时我们会发现很多页面太长,需要通过拖动滚动条才能回到顶部,这个用户体验并不好,所以很多网站会在窗口右下角出现一个回到**回到顶部**的按钮,点击它便会回到顶部。怎么制作回到顶部这个效果呢,可以通过两种方法。\n- 使用**锚连接**,使用`\u003ca href=\"#\"\u003e\u003c/a\u003e`标签默认返回顶部。这种使用锚链接回到顶部效果:\n  - 优点:简单快速,没有任何兼容性问题;\n  - 缺点:视觉上不够直观(会突然快速回到页面顶部),用户体验不好。\n- 用JavaScript来实现\n\n我们主要来学习如何使用JavaScript实现。如何实现呢,我们在脑海里演绎这个场景:当我们打开一个页面,鼠标向下滚动到一定程度,会出现“回到顶部”这个按钮,我们点击按钮,页面会由快到慢滚动到顶部,最后停留在顶部。这里涉及到的知识点有:\n- DOM操作:\n  - `document.getElementById`  根据ID获取标签元素\n  - `document.documentElement.scrollTop` 滚动条的数值,可读写\n- 事件运用:\n  - `window.onload` 页面加载完毕后触发\n  - `onclick` 鼠标点击后触发\n  - `window.onscroll` 滚动条滚动触发\n- 定时器\n  - `setInterval()` 设置定时器,需传2个参数\n  - `clearInterval()` 关闭定时器,需传一个参数\n\n好了,根据以上知识点,首先在html中增加按钮\n\n``` html\n\u003ca href=\"javascript:;\" id=\"btn\" title=\"回到顶部\"\u003e\u003c/a\u003e\n```\n\n然后在层叠样式表里添加\n\n``` css\n#btn {\n  width:38px;\n  height: 38px;/*按钮大片大小*/\n  display: none;\n  /*在一个屏幕的可见范围内没必要显示“回到顶部”按钮,所以默认不显示*/\n  /*再通过当滚动条滚动超过一个屏幕时再用JavaScript显示按钮*/\n  position: fixed;\n  left:50%;\n  bottom:30px;/*固定定位,相对于屏幕的可见范围位置*/\n  background:url(http://paddingme.qiniudn.com/top.png) no-repeat;margin-left: 25%;\n  /*按钮背景图片,这里我用的是bootstrap官网的scrolltop图片*/\n}\n```\n\n好了重点是JavaScript可以这样写:\n\n``` javascript\nwindow.onload=function() {\n    var obtn=document.getElementById(\"btn\"); //取得标签元素\n    var clientHeight=document.documentElement.clientHeight;\n    //获得一个屏幕的高度即可视区域高度\n    var timer=null;//设置计时器\n    var isTop=true;//见最后注释\n\n    window.onscroll=function(){//滚动条滚动时触发\n        var osTop=document.documentElement.scrollTop||document.body.scrollTop;\n        //获得滚动条到顶部的高度,document.documentElement.scrollTop取得IE高度,\n        //document.body.scrollTop取得chrome高度\n        if(osTop\u003e=clientHeight){\n        //滚动条到顶部的高度大于等于一个屏幕高度时,显示“回到顶部”按钮\n            obtn.style.display=\"block\";\n        }else{\n            obtn.style.display=\"none\";//否则隐藏按钮\n        }\n\n        //下面为了实现当回到顶部的时候,用户想要停下来观看页面,\n        //滚动滚动条可以停下来中断回到顶部的效果。\n        if(!isTop) clearInterval(timer);\n        //如果没有到顶部,滚动条滚动,则关闭计时器,见最后注释\n            isTop=false; //置未到顶部,见最后注释\n    }\n\n    obtn.onclick= function() {\n        timer = setInterval(function() {//setInterval,30ms执行一次函数\n            var osTop=document.documentElement.scrollTop||document.body.scrollTop;\n            //获得滚动条到顶部的高度\n            var ispeed=Math.floor(-osTop/5);//速度\n            document.documentElement.scrollTop=document.body.scrollTop=osTop+ispeed;\n            //越来越小,写滚动条到顶部的高度,实现回到顶部由快到慢的效果。\n\n            isTop=true;//见最后注释\n\n            if(osTop==0)//到达顶部关闭计时器\n                clearInterval(timer);\n        },30)\n    }\n}\n\n//如何实现当回到顶部时候,想突然中断呢?首先设置全局变量var isTop=true;\n//则obtn.onclick事件中置isTop为true,\n//当点击按钮,则滚动条开始想顶部滚动,触发window.onscroll事件,\n//这里!istop为false不执行clearInterval(timer);\n//但是下面的isTop=false;再将isTop置为false。\n//这时候你人为滚动鼠标,则再次触发window.onscroll 事件,\n//此时!istop为true 执行clearInterval(timer);则滚动条停下来。\n\n```\n\n看看效果,啧啧啧,用户体验灰常到位,想停就停,不想停则由快到慢回到顶部,让用户有一个适应的心理预期,还可能看到想看的风景停下来驻足欣赏。所以你心动没,赶紧打开[回到顶部效果](http://www.imooc.com/learn/65) 学习一下,涨涨姿势。\n\n今天暂时学习到这里,我还是乖乖的滚去写论文了。\n","author":{"url":"https://github.com/paddingme","@type":"Person","name":"paddingme"},"datePublished":"2014-11-25T14:28:53.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/13/Learning-JavaScript/issues/13"}

route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:ca2a2056-7a6a-16e9-9efe-851d3089462e
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id9DF8:8F86C:4DBE75:6BA1E7:6A610A57
html-safe-nonce4f60357640b809ab128f9f4c3def54efd28c76e6dfa940bca345ed0e63f5d779
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5REY4OjhGODZDOjREQkU3NTo2QkExRTc6NkE2MTBBNTciLCJ2aXNpdG9yX2lkIjoiNDI4NzY3MTg2MjA0MDc5MTYzOSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmac0155797ea23aa8f31ea7a02e8894bcc91318ec0897942dff74e3b363b46d4444
hovercard-subject-tagissue:50033040
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/paddingme/Learning-JavaScript/13/issue_layout
twitter:imagehttps://opengraph.githubassets.com/1658c4515d7470b9bf6c2f537509e44d82e1dd52f4f14b1e9ba67b74b87449d8/paddingme/Learning-JavaScript/issues/13
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/1658c4515d7470b9bf6c2f537509e44d82e1dd52f4f14b1e9ba67b74b87449d8/paddingme/Learning-JavaScript/issues/13
og:image:alt用 JavaScript 实现动画效果 动画的基础知识 如前文所说,如果我们想随着时间的变化而不断改变某个元素的样式,目前 CSS 尚且无能为力,只能使用 JavaScript。JavaScript 能够按照预定的时间间隔重复调用一个函数,而这意味着我们可以随着世家的推移而不断改变某个某个元素的样式。 动画是样式随时间变化的完美例子之一,简单的说,动画就是让元素的位置随着时间而不断的发生变化...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamepaddingme
hostnamegithub.com
expected-hostnamegithub.com
None86be97fd69ac233b46e43a5eed04ceac6498abe1efaf1af3b9573eb216019c05
turbo-cache-controlno-preview
go-importgithub.com/paddingme/Learning-JavaScript git https://github.com/paddingme/Learning-JavaScript.git
octolytics-dimension-user_id5771087
octolytics-dimension-user_loginpaddingme
octolytics-dimension-repository_id25473884
octolytics-dimension-repository_nwopaddingme/Learning-JavaScript
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id25473884
octolytics-dimension-repository_network_root_nwopaddingme/Learning-JavaScript
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
releasef80280886562a52fe1f6b9cab9f9c1dc3b9a78a2
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/paddingme/Learning-JavaScript/issues/13#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpaddingme%2FLearning-JavaScript%2Fissues%2F13
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub Copilot appDirect agents from issue to mergehttps://github.com/features/ai/github-app
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
Code QualityEnforce quality at mergehttps://github.com/features/code-quality
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
View all resourceshttps://github.com/resources
GitHub SponsorsFund open source developershttps://github.com/open-source/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/open-source/accelerator
GitHub Starshttps://stars.github.com
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpaddingme%2FLearning-JavaScript%2Fissues%2F13
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=paddingme%2FLearning-JavaScript
Reloadhttps://github.com/paddingme/Learning-JavaScript/issues/13
Reloadhttps://github.com/paddingme/Learning-JavaScript/issues/13
Reloadhttps://github.com/paddingme/Learning-JavaScript/issues/13
paddingme https://github.com/paddingme
Learning-JavaScripthttps://github.com/paddingme/Learning-JavaScript
Notifications https://github.com/login?return_to=%2Fpaddingme%2FLearning-JavaScript
Fork 13 https://github.com/login?return_to=%2Fpaddingme%2FLearning-JavaScript
Star 28 https://github.com/login?return_to=%2Fpaddingme%2FLearning-JavaScript
Code https://github.com/paddingme/Learning-JavaScript
Issues 30 https://github.com/paddingme/Learning-JavaScript/issues
Pull requests 0 https://github.com/paddingme/Learning-JavaScript/pulls
Actions https://github.com/paddingme/Learning-JavaScript/actions
Projects https://github.com/paddingme/Learning-JavaScript/projects
Wiki https://github.com/paddingme/Learning-JavaScript/wiki
Security and quality 0 https://github.com/paddingme/Learning-JavaScript/security
Insights https://github.com/paddingme/Learning-JavaScript/pulse
Code https://github.com/paddingme/Learning-JavaScript
Issues https://github.com/paddingme/Learning-JavaScript/issues
Pull requests https://github.com/paddingme/Learning-JavaScript/pulls
Actions https://github.com/paddingme/Learning-JavaScript/actions
Projects https://github.com/paddingme/Learning-JavaScript/projects
Wiki https://github.com/paddingme/Learning-JavaScript/wiki
Security and quality https://github.com/paddingme/Learning-JavaScript/security
Insights https://github.com/paddingme/Learning-JavaScript/pulse
【JavaScript】【学习心得】学习 JavaScript 第九天https://github.com/paddingme/Learning-JavaScript/issues/13#top
Mu-Help-Planhttps://github.com/paddingme/Learning-JavaScript/issues?q=state%3Aopen%20label%3A%22Mu-Help-Plan%22
https://github.com/paddingme
paddingmehttps://github.com/paddingme
on Nov 25, 2014https://github.com/paddingme/Learning-JavaScript/issues/13#issue-50033040
http://codepen.io/paddingme/pen/jEPBqghttp://codepen.io/paddingme/pen/jEPBqg
回到顶部效果http://www.imooc.com/learn/65
回到顶部效果http://www.imooc.com/learn/65
Mu-Help-Planhttps://github.com/paddingme/Learning-JavaScript/issues?q=state%3Aopen%20label%3A%22Mu-Help-Plan%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.