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

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值