首页 > 代码库 > 插件方法总结

插件方法总结

//淡入淡出轮播
function fadeInOut(imgWrap,text,btn){
var speed = 2000,
interval = 4000,
nowIndex = 0,
timer = null,
oPrev = btn.find(".prev"),
oNext = btn.find(".next"),
oP = text.find("p"),
length = imgWrap.find(‘li‘).length;
imgWrap.find(‘li‘).eq(0).show();
imgWrap.hover(function(){
clearInterval(timer);
},function(){
timer = setInterval(cslProgcess,interval);
});
oNext.click(function(){
clearInterval(timer);
nowIndex ++;
oP.eq(nowIndex%length).show().siblings().hide();
imgWrap.find(‘li‘).hide().eq(nowIndex%length).fadeIn();
timer = setInterval(cslProgcess,interval);
});
oPrev.click(function(){
clearInterval(timer);
nowIndex --;
if (nowIndex<0) {
nowIndex == length-1;
}
oP.eq(nowIndex%length).show().siblings().hide();
imgWrap.find(‘li‘).fadeOut().eq(nowIndex%length).fadeIn();
timer = setInterval(cslProgcess,interval);
});
function cslProgcess(){
nowIndex ++;
oP.eq(nowIndex%length).show().siblings().hide();
imgWrap.find(‘li‘).fadeOut().eq(nowIndex%length).fadeIn(speed);
}
timer = setInterval(cslProgcess,interval);
}

 

//间距设置

function setSpace(val,div,attr,old){
var curHeight = $(window).height();
div.css(attr,old*curHeight/val+‘px‘);
}


//密码焦点框
function loginInpt(inputbox,blurcontent,type){
var typeVal = type||‘‘;
inputbox.focus(function(){
if (typeVal==‘password‘) {
inputbox.attr(‘type‘,‘password‘);
}
if($(this).val()==blurcontent){
$(this).val(‘‘);
}
});
inputbox.blur(function(){
if($(this).val()==‘‘){
if (typeVal==‘password‘) {
inputbox.attr(‘type‘,‘text‘);
}
$(this).val(blurcontent);
}
});
}

 


//判断手指滑动方向
var doc = document;
doc.addEventListener("touchstart", startTouchScroll, false);
doc.addEventListener("touchmove", moveTouchScroll, false);
doc.addEventListener("touchend", endTouchScroll, false);
var startY, endY, startX, endX;
function startTouchScroll(event)
{
var touch = event.touches[0];
startX = touch.pageX;
startY = touch.pageY;
}
function moveTouchScroll(event)
{
var touch = event.touches[0];
endX = touch.pageX;
endY = touch.pageY;
}
function endTouchScroll(event)
{
//在这里判断也可以
/*var touch = event.touches[0];
if(!touch){
return ;
}
endX = touch.pageX;
endY = touch.pageY;
*/

//判断移动的点,1为手指向下滑动,-1为手指向上滑动
var scrollDirection = (endY - startY) > 0 ? 1 : -1;
//判断移动的点,1为手指向右滑动,-1为手指向左滑动
var scrollTranslation = (endX - startX) > 0 ? 1 : -1;
//计算滑动距离
var scrollDistance = Math.abs(endY - startY);

}

 

 

$(function(){
//根据行数来限制文字溢出
function wordRowLimt(node, row) {
var _height = parseInt(node.css(‘line-height‘))*row; //限制的高度
for (var i = 0; i < node.length; i++) {//多个节点遍历
var old_str = node.eq(i).text(); //节点文本
for (var j = 0; j < old_str.length; j++) {
node.eq(i).text(old_str.substring(0, j));
if (node.eq(i).height() > _height) { //截取字符串
new_str = old_str.substring(0, j-2)+"...";
node.eq(i).text(new_str);
break;
}
}
}
}

//适应屏幕
function setDiv(div){
var _windowW=$(window).width()>1000?$(window).width():1000;
var _windowH=$(window).height()>650?$(window).height():650;
div.height(_windowH);
div.width("100%");
}
function setImg(img){
var _windowW=$(window).width()>1024?$(window).width():1024;
var _windowH=$(window).height()>650?$(window).height():650;
var imgH=img.height(),
imgW=img.width();

if(_windowW/_windowH>imgW/imgH){
img.width(_windowW);
img.height(_windowW*(imgH/imgW));
}else{
img.height(_windowH);
img.width(_windowH*(imgW/imgH));
}
var nh=img.height(),
nw=img.width();

img.css({
left:(_windowW-nw)/2,
top:(_windowH-nh)/2
})
}


//input默认值替换
$(".survey-div ul li").click(function(){
$(this).addClass("on").siblings().removeClass("on");
$(this).find("input").attr("checked",true);
$(this).siblings().find("input").attr("checked",false);
});
$(".survey-div1 ul li").click(function(){
var checked = $(this).find("input").attr("checked");
$(this).toggleClass("on");
if (checked) {
$(this).attr("checked",false);
}else{
$(this).attr("checked",true);
}
});


//返回到默认的值
function backFirst(parent,child,index){
parent.hover(function(){
child.mouseover(function(){
$(this).addClass("on").siblings().removeClass("on");
});
},function(){
child.eq(index).addClass("on").siblings().removeClass("on");
});
}

//字数限制
function txtLimit(div, num, point) {
for (var i = 0; i < div.length; i++) {
var news_txt = div.eq(i).text();
if (news_txt.length >= num) {
var txt = news_txt.substr(0, num) + point;
div.eq(i).text(txt);
}
}
}
//超出添加<a>
function addHref(div, num, point,texts) {
div.each(function(){
var news_txt = $(this).text();
if (news_txt.length >= num) {
var txt = news_txt.substr(0, num) + point;
$(this).text(txt);
$(this).append(‘<a href="">‘+texts+‘</a>‘);
}
})
}

// 全选/全不选
function selectBox(allBtn,notBtn,itemBox){
allBtn.click(function(){
itemBox.find("input").each(function(){
$(this).prop("checked",true);
});
});
notBtn.click(function(){
itemBox.find("input").each(function(){
$(this).prop("checked",false);
});
});
}

//选项卡
function tabItem($item,$box,event){
$box.eq(0).show();
$item.on(event,function(){
var index = $(this).index();
$(this).addClass("on").siblings().removeClass("on");
$box.eq(index).show().siblings().hide();
});
}

//下拉框
function getText(parent){
parent.click(function(){
// window.event?window.event.cancelBubble=true:event.stopPropagation();
var e=getEvent();
if(window.event){
//e.returnValue=http://www.mamicode.com/false;//阻止自身行为
e.cancelBubble=true;//阻止冒泡
}else if(e.preventDefault){
//e.preventDefault();//阻止自身行为
e.stopPropagation();//阻止冒泡
}
var _this = $(this);
_this.find("ul").slideToggle();
_this.find("ul li").click(function(){
var liText = $(this).text();
_this.find("input").val(liText);
});
});
$("body").click(function(){
parent.find("ul").slideUp();
})
}
//获取事件
function getEvent(){
if(window.event) {return window.event;}
func=getEvent.caller;
while(func!=null){
var arg0=func.arguments[0];
if(arg0){
if((arg0.constructor==Event || arg0.constructor ==MouseEvent
|| arg0.constructor==KeyboardEvent)
||(typeof(arg0)=="object" && arg0.preventDefault
&& arg0.stopPropagation)){
return arg0;
}
}
func=func.caller;
}
return null;
}

//搜索框
function focus(inputbox,blurcontent){
inputbox.focus(function(){
if($(this).val()==blurcontent){
$(this).val(‘‘);
}
});
inputbox.blur(function(){
if($(this).val()==‘‘){
$(this).val(blurcontent);
}
});
}

//回到顶部
function backTop(btn){
btn.click(function(){
var timer = null;
var speed = 2;
var interval = 50;
function topMove(){
var scrollTop = $(document).scrollTop();
scrollTop = scrollTop/speed;
if (scrollTop<1) {
scrollTop = 0;
clearInterval(timer);
}
$(document).scrollTop(scrollTop);
}
timer = setInterval(topMove,interval);
});
}

// 日期展示
function dateShow(){
var dateStr = getDate();
var year = dateStr.substring(0,4);
var month = dateStr.substring(4,6);
var day = dateStr.substring(6,8);
$(".csl-day").text(day);
$(".csl-year").text(year+"-"+month);
}
//日期
function getDate(){
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth()+1;
var day = date.getDate();
month<10?month="0"+month:month=""+month;
day<10?day="0"+day:day=""+day;
return year+month+day;
}
});

插件方法总结