分享一下10个常用jquery片段
1. 图片预加载
(function($) {var cache = [];// Arguments are image paths relative to the current page.$.preLoadImages = function() {var args_len = arguments.length;for (var i = args_len; i--;) {var cacheImage = document.createElement('img');cacheImage.src = arguments[i];cache.push(cacheImage);}}jQuery.preLoadImages("image1.gif", "/path/to/image2.png");
2. 在新窗口打开链接 (target=”blank”)
$('a[@rel$='external']').click(function(){this.target = "_blank"; });/*Usage:<a href="http://www.catswhocode.com" rel="external">catswhocode.com</a> */
3. 当支持 JavaScript 时为 body 增加 class
/* 该代码只有1行,但是最简单的用来检测浏览器是否支持 JavaScript 的方法,如果支持 JavaScript 就在 body 元素增加一个 hasJS 的 class */ $('body').addClass('hasJS');
4. 平滑滚动页面到某个锚点
$(document).ready(function() {$("a.topLink").click(function() {$("html, body").animate({scrollTop: $($(this).attr("href")).offset().top + "px"}, {duration: 500,easing: "swing"});return false;}); });
5. 鼠标滑动时的渐入和渐出
$(document).ready(function(){$(".thumbs img").fadeTo("slow", 0.6); // This sets the opacity of the thumbs to fade down to 60% when the page loads$(".thumbs img").hover(function(){$(this).fadeTo("slow", 1.0); // This should set the opacity to 100% on hover},function(){$(this).fadeTo("slow", 0.6); // This should set the opacity back to 60% on mouseout}); });
6. 制作等高的列
var max_height = 0; $("div.col").each(function(){if ($(this).height() > max_height) { max_height = $(this).height(); } }); $("div.col").height(max_height);
7. 在一些老的浏览器上启用 HTML5 的支持
(function(){if(!/*@cc_on!@*/0)return;var e = "abbr,article,aside,audio,bb,canvas,datagrid,datalist,details,dialog,eventsource,figure,footer,header,hgroup,mark,menu,meter,nav,output,progress,section,time,video".split(','),i=e.length;while(i--){document.createElement(e[i])} })()//然后在head中引入该js <!--[if lt IE 9]> <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script> <![endif]-->
8. 测试浏览器是否支持某些 CSS3 属性
var supports = (function() {var div = document.createElement('div'),vendors = 'Khtml Ms O Moz Webkit'.split(' '),len = vendors.length;return function(prop) {if ( prop in div.style ) return true;prop = prop.replace(/^[a-z]/, function(val) {return val.toUpperCase();});while(len--) {if ( vendors[len] + prop in div.style ) {// browser supports box-shadow. Do what you need.// Or use a bang (!) to test if the browser doesn't.return true;}}return false;}; })();if ( supports('textShadow') ) {document.documentElement.className += ' textShadow';
9. 获取 URL 中传递的参数
$.urlParam = function(name){var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);if (!results) { return 0; }return results[1] || 0; }
10. 禁用表单的回车键提交
$("#form").keypress(function(e) {if (e.which == 13) {return false;} });
http://www.open-open.com/code/view/1439432223083
转载于:https://www.cnblogs.com/zhujiasheng/p/6185584.html
分享一下10个常用jquery片段相关推荐
- 经验分享:10个简单实用的 jQuery 代码片段
尽管各种 JavaScirpt 框架和库层出不穷,jQuery 仍然是 Web 前端开发中最常用的工具库.今天,向大家分享我觉得在网站开发中10个简单实用的 jQuery 代码片段. 您可能感兴趣的相 ...
- 常用jQuery代码分享
为什么80%的码农都做不了架构师?>>> 常用jQuery代码分享 1.查看浏览器信息 navigator.userAgent 检索浏览器信息包括哪些字符 例如: if (na ...
- GBin1分享的10个吸引眼球的jQuery插件
日期:2011/10/11 原文:jquery4u.com 编译:GBin1.com GBin1今天特意选取了10个非常吸引眼球的jQuery小组件和插件与大家分享,其中包括图片廊,文本功能提升,及其 ...
- node 压缩图片_推荐10个常用的图片处理小帮手(下)「值得收藏」
作者: semlinker 转发链接:https://mp.weixin.qq.com/s/i3ynTtPJOECoAYfqHFoo3Q 前言 本文给小伙伴们隆重介绍用于图片处理的十个 「" ...
- 【精心挑选】10款基于 jQuery 的图片360度旋转插件
之前的文章向大家分享了实现网站功能的各种优秀的 jQuery 插件,今天这篇文章向大家推荐10款基于 jQuery 的图片360度旋转插件,同时还有非常详细的制作教程可以学习和参考.图片旋转展示是一种 ...
- 前端:分享一些实用的JS代码片段
今天给大家分享一些实用的JS代码片段,有需要的朋友欢迎收藏! 1.获取浏览器的版本 function getBrowser() {var UserAgent = navigator.userAgent ...
- html css 圆形按钮 仿uc,10款基于jquery的web前端动画特效
1.jQuery横向手风琴图片切换动画 之前我们为大家分享过很多款基于jQuery和CSS3的手风琴菜单和手风琴焦点图插件,比如CSS3响应式垂直手风琴菜单和jQuery横向手风琴图片展示插件.今天要 ...
- 精心挑选10款基于 jQuery 的图片360度旋转插件
之前的文章向大家分享了实现网站功能的各种优秀的 jQuery 插件,今天这篇文章向大家推荐10款基于 jQuery 的图片360度旋转插件,同时还有非常详细的制作教程可以学习和参考.图片旋转展示是一种 ...
- 10款基于jquery实现的超酷动画源码
1.jQuery二级下拉菜单 下拉箭头翻转动画 之前我们分享过不少基于jQuery的二级下拉菜单,甚至是多级的下拉菜单,比如这款jQuery/CSS3飘带状多级下拉菜单就非常华丽.但今天要介绍的这款j ...
最新文章
- 121-Best Time to Buy and Sell Stock
- mysql创建数据库选择_mysql创建、选择并使用数据库
- 微软可能是全球最大的开源贡献者,但开发者似乎并不买账​​​​​​​
- 【机器视觉】 endif算子
- mysql explain的使用(优化查询)
- 一个10篇一作SCI博士的走心分享----宏组学研究之“道” (完整收藏版)!!
- 华为Android10版怎么截屏,安卓手机截图方法 华为手机如何截图 - 云骑士一键重装系统...
- Google 封杀我写的 Web 浏览器!
- [转]asp 无法连接 access,出现 -2147467259 未指定的错误
- unity中Rotation * Position的意义
- Linux学习12—文件服务
- 九大知识领域与五大过程组
- 在Lenovo T61笔记本上安装Windows XP
- 网站受到流量攻击怎么办
- Adobe read X安装过程中出现无法将数值DisableExceptionChainValidation写入键
- YOLOv5、CNN、SVM实现车牌检测
- AI前沿论坛会议—文字智能和游戏智能总结篇
- Linux模板机以及搭建集群操作
- 干货!教你4种文件恢复方法,再也不怕误删除!
- 如何设置UILabel的内边距?