Skip to content

Instantly share code, notes, and snippets.

@boboidream
Last active May 26, 2017 12:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boboidream/97fb461c3aad01830cb8754a8a1842b0 to your computer and use it in GitHub Desktop.
Save boboidream/97fb461c3aad01830cb8754a8a1842b0 to your computer and use it in GitHub Desktop.
javascript gist
$(window).on 'scroll', ->
nowScrollTop = $(this).scrollTop()
if nowScrollTop > 80 & nowScrollTop > lastScrollTop
$gFilter.fadeOut()
else if nowScrollTop < lastScrollTop
$gFilter.fadeIn()
lastScrollTop = nowScrollTop
# 内部滚动不影响外部 BODY 滚动
$('.lux-toc').on 'wheel', (e) ->
d = e.originalEvent.wheelDelta || -e.originalEvent.detail
if d > 0 then dir = 'up' else dir = 'down'
if dir is 'up' and this.scrollTop is 0
e.preventDefault()
if dir is 'down' and this.scrollTop > this.scrollHeight - this.offsetHeight - 50
e.preventDefault()
// 智能识别 http | ftp 网址
function recognitionURL(str) {
return str.replace(/(http:\/\/[^<^\s]+|ftp:\/\/[^<^\s]+|https:\/\/[^<^\s]+)(<\/div>|\s*?)/g, function(whole, b, c) {
return '<a href=' + b + ' target="_blank">' + b + '</a>' + c
})
}
# URLCtrl render
URLCtrl = do ->
URLObj = {}
URLObj.href = location.href
URLObj.origin = location.origin
URLObj.search = do ->
searchObj = {}
searchArray = location.search.slice(1).split('&')
for item in searchArray
tmp = item.split('=')
key = tmp[0]
val = tmp[1]
searchObj[key] = val if key
return searchObj
getNewURL = ->
searchString = '?'
for key,val of URLObj.search
searchString += (key + '=' + val + '&')
if searchString.slice(-1) == '&'
searchString = searchString.slice(0, -1)
return URLObj.origin + searchString
return {
setSearch: (key, val) ->
URLObj.search[key] = val
if history.pushState
history.pushState({}, '', getNewURL())
}
# 比如字符串: 我是中度的的的的度的的的的度的的的的度的的的的的.jpg
# 设定最大宽度 width: 300
# 结果:我是中度的的的的...的的的的.jpg
getShortName: (name, width) ->
separator = '...'
$wrap = $ "<span id='getNameWrap' style='display:none'>#{name}</span>"
$('body').append $wrap
return name if $wrap.width <= width
shortName = separator
shortNameTest = separator
textArray = name.split ''
nameWidth = $wrap.text(shortNameTest).width()
insertHead = true
while nameWidth < width
shortName = shortNameTest
if insertHead
shortNameTest = shortNameTest + textArray.shift()
else
shortNameTest = textArray.pop() + shortNameTest
nameWidth = $wrap.text(shortNameTest).width()
insertHead = !insertHead
$wrap.remove()
nameArray = shortName.split(separator)
nameArray[1] + separator + nameArray[0]
  • js
# 强制 reflow
forceReflow = ($el) ->
	el = if $el instanceof $ then $el[0] else $el
	el.offsetHeight

# $(el).one() 绑定一次使用事件
$(document).on 'click', '.userpanel-avatar', (e) ->
	isOpen = $uPanel.hasClass 'open'
	if isOpen
		$uPanel.removeClass 'open'
		$uPanelDrop.one 'transitionend', (e) ->
			$uPanelDrop.off().hide()
	else
		$uPanelDrop.show() && forceReflow $uPanelDrop
		$uPanel.addClass 'open'
  • css
.list_filter .filter_menu {
	display: none;
	opacity: 0;
	transform: translate(0, 20px);
	transition: all .4s ease;
}
.list_filter .filter_menu.open {
	opacity: 1;
	transform: translate(0, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment