Skip to content

Instantly share code, notes, and snippets.

@alanerzhao
Created November 25, 2014 02:07
Show Gist options
  • Save alanerzhao/2cded6ee0479a54f6a57 to your computer and use it in GitHub Desktop.
Save alanerzhao/2cded6ee0479a54f6a57 to your computer and use it in GitHub Desktop.
base.js
/**
* @file base.js
* @brief ui dom handle
* @author baozi
* @version
* @date 2014-11-19
*/
define("base",["jquery"],function(require, exports, module) {
var $ = require("jquery");
var base = {
placeholder : function (dom,txt) {
$(dom).on('focus', function() {
$(this).css("color","#000")
if(txt) {
$(this).data('defaultText', txt);
}
if (!$(this).data('defaultText')) {
$(this).data('defaultText', $(this).val());
}
if ($(this).val()==$(this).data('defaultText')) {
$(this).val('');
}
});
$(dom).on('blur', function() {
$(this).css("color","#ccc")
if ($(this).val()=='') {
$(this).val($(this).data('defaultText'));
}
});
},
active: function (dom,child,cur) {
var hash = location.hash;
$(dom).on("click",child,function(event) {
$(this).parent().siblings().find(child).removeClass(cur);
$(this).addClass(cur);
});
$(dom).find(child).each(function(idx,obj) {
//TODO 处理hash
if ($(obj).attr("href") == hash) {
$(obj).addClass(cur)
}
})
},
digger: function (obj,dis) {
var artbtn = ".art-pinglun-btn";
var diggercont = ".J_digger_cont";
$(obj).on("click",".art-dianping",function() {
$(this).parent(artbtn).siblings(diggercont).addClass("J_cont_show");
})
},
//滚动处理
anchor: function (_startElem, _endElem, _speed) {
var item = $(_endElem);
$(_startElem).each(function(){
var that = $(this);
that.click(function () {
index = that.index();
$('html,body').animate({
scrollTop: item.eq(index).offset().top
},_speed || 0);
})
})
},
scrollTop: function () {
var linkTop = $("<a class='feed-scroll-top' href='javascript:;'></a>");
$("body").append(linkTop);
var winHeight = $(window).height();
$(window).on("scroll",debounce(function () {
var top = $(this).scrollTop();
if(top > winHeight) {
linkTop.fadeIn();
} else {
linkTop.fadeOut();
}
},300))
linkTop.click(function(){
$('html, body').animate({scrollTop : 0},800);
return false;
});
}
};
// Underscore help
function debounce(func, wait, immediate) {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
}
if (typeof module != "undefined" && module.exports) {
module.exports = base;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment