收集的一些较冷但却很有用的jquery

来源:互联网 发布:公共网络怎么改成家庭 编辑:程序博客网 时间:2024/05/20 19:28

1、 禁用Jquery(动画)效果

jQuery.fx.off = true;

2、使用自己的 Bullets(这个有一丁点儿的小技巧)

//这里是js代码  也就是给每个ul添加一个类名  然后给ul的子li前面添加html 你想要使用的Bullets$("ul").addClass("Replaced");$("ul > li").prepend("‒ "); //在css里面这样写 ul.Replaced { list-style : none; }

3、写自己的选择器

复制代码
$.extend($.expr[':'], {       moreThen1000px: function(a) {           return $(a).width() > 1000;      }   });  $('.box:moreThen1000px').click(function() {      // creating a simple js alert box      alert('The element that you have clicked is over 1000 pixels wide');  });
复制代码

4、使元素屏幕居中

复制代码
jQuery.fn.center = function () {      this.css("position","absolute");      this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");      this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");      return this;  }//宽度居中的位置应该就是窗口宽度减去当前元素宽度再除以2 最后减掉左部滚动宽度 高度同理$("#id").center();
复制代码

5、获得鼠标指针XY值

$().mousemove(function(e){     //display the x and y axis values inside the div with the id XY    $('#XY').html("X Axis : " + e.pageX + " | Y Axis " + e.pageY);  });

6、返回页面顶部功能

复制代码
$('a[href*=#]').click(function() { if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {//为什么会用replace呢  后来想想明白了 因为很多url里面会有双重的/这样replace之后再比较比如好   var $target = $(this.hash);   $target = $target.length && $target|| $('[name=' + this.hash.slice(1) +']');//写这么晦涩的语法  意思就是说当前a元素的hash长度必须大于0并且不为undefined   则去找name等于获得的hash值的元素   if ($target.length) {  var targetOffset = $target.offset().top;//获得目标元素的高度  $('html,body').animate({scrollTop: targetOffset}, 900);

return false; } } });
复制代码

7、在新窗口中打开链接

复制代码
//Example 1: Every link will open in a new window   $('a[href^="http://"]').attr("target", "_blank");     //Example 2: Links with the rel="external" attribute will only open in a new window   $('a[@rel$='external']').click(function(){      this.target = "_blank";   });
复制代码
0 0
原创粉丝点击