blogs-js's People
blogs-js's Issues
原生JS实现元素平滑滚动至顶部
var btn = document.querySelector('#button')
btn.onclick = function() {
var clock = setInterval(function(){
var distance = document.querySelector('#scroll-ul').scrollTop //获取需要滚动的元素距离顶部的距离
if(distance > 0){
document.querySelector('#scroll-ul').scrollTop = distance-50 //这里的50是每次interval滚动的距离,scrollTop并不是一个方法
}else{
clearInterval(clock)
}
},50)
}
其它相关的API:
window.scrollTo(x,y) 文档滚动,是一个方法,但是只有window能用
element.scrollLeft 距离左侧的滚动距离
online
判断PC浏览器类型的JS代码
function getExplorer() {
var explorer = window.navigator.userAgent //重点在这里
if (explorer.indexOf('MSIE') >= 0) {
alert('这是IE浏览器')
} else if (explorer.indexOf('Chrome') >= 0) {
alert('这是谷歌浏览器')
} else if (explorer.indexOf('FireFox') >= 0) {
alert('这是火狐浏览器')
} else if (explorer.indexOf('Safari') >= 0) {
alert('这是safari浏览器')
}
}
getExplorer()
判断手机浏览器类型的代码(不完整需完善)
function getMobileExplorer() {
var explorer = window.navigator.userAgent
var uc = RegExp('Android.+ucbrowser', 'gi')
var sogou = RegExp('Android.+SogouMobileBrowser', 'gi')
var baidu = RegExp('Android.+baidubrowser', 'gi')
if (uc.test(explorer)) {
alert('这是安卓UC浏览器')
} else if (sogou.test(explorer)) >= 0) {
alert('这是安卓手机搜狗浏览器')
} else if (baidu.test(explorer)){
alert('这是安卓手机百度浏览器')
}
}
getMobileExplorer()
用jQuery实时获取滚动距离
$('#container').on('scroll', function(){
var $distance = $('#container').scrollTop()
console.log($distance)
})
下拉列表点击页面其它地方消失的一种办法(阻止冒泡)
document.querySelector('.listDown').chidren.addEventListener('click',function (event) {
event.stopPropagation()
document.getElementsByClassName('listDown')[0].style.display = 'none'
})
document.addEventListener('click', function () {
document.getElementsByClassName('listDown')[0].style.display = 'none'
})
这是一种冒泡的用法
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.