本文主要为大家分享一篇关于jquery的实用技巧的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧。以下十项jquery示例可以帮助大家的web设计项目顺利实现效率提升。
检测ie浏览器
在进行css设计时,ie浏览器对开发者及设计师而言无疑是个麻烦。尽管ie6的黑暗时代已经过去,ie浏览器家族的人气亦在不断下滑,但我们仍然有必要对其进行检测。当然,以下片段亦可用于检测其它浏览器。
$(document).ready(function() {
if (navigator.useragent.match(/msie/i) ){
alert('i am an old fashioned internet explorer');
}
});
平滑滚动至页面顶部
以下是jquery最为常见的一种实现效果:点击一条链接以平滑滚动至页面顶部。虽然没什么新鲜感可言,但每位开发者几乎都用得上。
$(a[href='#top']).click(function() {
$(html, body).animate({ scrolltop: 0 }, slow);
return false;
});
保持始终处于顶部
以下代码片段允许某一元素始终处于页面顶部。可以想见,其非常适合处理导航菜单、工具栏或者其它重要信息。
$(function(){
var $win = $(window)
var $nav = $('.mytoolbar');
var navtop = $('.mytoolbar').length && $('.mytoolbar').offset().top;
var isfixed=0;
processscroll()
$win.on('scroll', processscroll)
function processscroll() {
var i, scrolltop = $win.scrolltop()
if (scrolltop >= navtop && !isfixed) {
isfixed = 1
$nav.addclass('subnav-fixed')
} else if (scrolltop <= navtop && isfixed) {
isfixed = 0
$nav.removeclass('subnav-fixed')
}
}
替换html标签
jquery能够非常轻松地实现html标签替换,而这也将为我们带来更多新的可能。
$('li').replacewith(function(){
return $("<p />).append($(this).contents());
});
检测屏幕宽度
现在移动设备的人气几乎已经超过了传统计算机,因此对小型屏幕的尺寸进行检测就变得非常重要。幸运的是,我们可以利用jquery轻松实现这项功能。
var responsive_viewport = $(window).width();
/* if is below 481px */
if (responsive_viewport < 481) {
alert('viewport is smaller than 481px.');
} /* end smallest screen */
自动修复损坏图片
如果大家的站点非常庞大而且已经上线数年,那么其中或多或少会出现图片损坏的情况。这项功能可以检测损坏图片并根据我们的选择加以替换。
$('img').error(function(){
$(this).attr('src', 'img/broken.png');
});
检测复制、粘贴与剪切操作
利用jquery,大家可以非常轻松地检测到选定元素的复制、粘贴与剪切操作。
$(#texta).bind('copy', function() {
$('span').text('copy behaviour detected!')
});
$(#texta).bind('paste', function() {
$('span').text('paste behaviour detected!')
});
$(#texta).bind('cut', function() {
$('span').text('cut behaviour detected!')
});
自动为外部链接添加target=“blank”属性
在链接至外部站点时,大家可能希望使用target=blank属性以确保在新的选项卡中打开页面。问题在于,target=blank属性并未经过w3c认证。jquery能够帮上大忙:以下片段能够检测当前链接是否指向外部,如果是则自动为其添加target=blank属性。
var root = location.protocol + '//' + location.host;
$('a').not(':contains(root)').click(function(){
this.target = _blank;
});
悬停时淡入/淡出
又是另一项“经典”效果,大家可以利用以下片段随时加以运用。
$(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
});
});
禁用文本/密码输入中的空格
无论是电子邮件、用户名还是密码,很多常见字段都不需要使用空格。以下代码能够轻松禁用选定输入内容中的全部空格。
$('input.nospace').keydown(function(e) {
if (e.keycode == 32) {
return false;
}
});
以上就是关于jquery的实用技巧的详细内容。