热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

10个厚脸皮的快速jQuery片段

h3{padding-top:25px!important;padding-bottom:5px!important;}这是大约10个厚脸皮的jQuery代码段的集合。我相信

这是大约10个厚脸皮的jQuery代码段的集合。 我相信您会发现它们很有用,请尽情享受!

快速射击jQuery片段!

1.使所有图像变为灰度

此快速功能和摘要使用HTML5 canvas和jQuery将页面上的所有图像从彩色变为灰色。

greyscale-image

// Grayscale image using HTML5 canvas method
function grayscale(src){var canvas = document.createElement('canvas');var ctx = canvas.getContext('2d');var imgObj = new Image();imgObj.src = src;canvas.width = imgObj.width;canvas.height = imgObj.height; ctx.drawImage(imgObj, 0, 0); var imgPixels = ctx.getImageData(0, 0, canvas.width, canvas.height);for(var y = 0; y }//Make all images on page Greyscale!
$('img').each(function(){var el = $(this);el.css({"position":"absolute"}).wrap("

").clone().addClass('img_grayscale').css({"position":"absolute","z-index":"998","opacity":"0"}).insertBefore(el).queue(function(){var el = $(this);el.parent().css({"width":this.width,"height":this.height});el.dequeue();});this.src = grayscale(this.src);
});

2. Create element using object literal syntax

This is not the usual way to do things when setting properties on DOM elements so very cheeky!

$("

",
{id: "test",name: "test",class: "test-class",css: {width: "100px",height: "100px",backgroundColor: "#fff"}
});

3. Tell IE6 User upgrade thier browser

Nicely Tell IE6 Noobs to upgrade thier browser by simply adding this code inside your opening body tag. This is what they see:

Your browser is ancient! Upgrade to a different browser or install Google Chrome Frame to experience this site.

4. Shorten Links with bit.ly using jQuery

Thanks to James Cridland for this cheeky little URL shortener code snippet. You’ll need a bit.ly api key to use this one, but don’t worry it’s a completely free service.

function get_short_link($url)
{$bitly_login="yourloginname";$bitly_apikey="yourapikey";$api_call = file_get_contents("http://api.bit.ly/shorten?version=2.0.1&longUrl=".$url."&login=".$bitly_login."&apiKey=".$bitly_apikey);$bitlyinfo=json_decode(utf8_encode($api_call),true);if ($bitlyinfo['errorCode']==0) {return $bitlyinfo['results'][urldecode($url)]['shortUrl'];} else {return false;}
}//Usage: get_short_link("http://jquery4u.com");

5. Quick Format Currency

This cheeky little snippet solves a complex show currency problem in a simple way which is also works cross browser!

function formatCurrency(num) {num = isNaN(num) || num === '' || num === null ? 0.00 : num;return parseFloat(num).toFixed(2);
}

6. Quick Caching Images

This snippet caches an image in the browser and then removes it so when it’s loaded into the DOM it doesn’t show those ugly missing image little red crosses. How cheeky!

cahced-images-console-log

//cache the user img
$('img').hide().appendTo('body').one('load', function() {console.log('Image: '+$(this).attr('src')+' is cached...');$(this).remove();
});

7. Z-Index Fix for Video Objects

If your having problems with z-index on video elements you can add a wmode parameter. Use this cheeky little snippet to fix that z-index problem! You can use “transparent” instead of “opaque” but the latter is less render intensive.

z-index-fix-for-chrome

//specific object
$('#videocontainerid object').prepend('

');//all objects
$('object').prepend('

');

8. Get last class of an element

This snippet gets the last class of a DOM element so if you had a DOM element with class=”class1, class2, class3″ it would return “class3”. Cool?

var lastElClass = $(element).attr("class").split("").slice(-1);

9. Remove Browser COOKIEs using Javascript

This collection of functions/snippets help you delete all browser COOKIEs using Javascript! Yes using Javascript, how cheeky!

get-clear-all-COOKIE-keys

//Browser COOKIEs are stored in Javascript here:

Javascript:document.COOKIE

//function to delete COOKIE (change expiry date)

function delCOOKIE(name)

{
document.COOKIE = name+’=; expires=Thu, 2 Aug 2001 20:47:11 UTC; path=/’

}

//function to get the keys for each COOKIE name

function getCOOKIEKeys()

{
// Separate key / value pairs
var COOKIEs = document.COOKIE.split(“;”),
index, keys = [];
for(i = 0; i }
var COOKIEKeys = getCOOKIEKeys();//delete all COOKIEs
for(i = 0; i {delCOOKIE(COOKIEKeys[i]);
}//check they are gone!
Javascript:document.COOKIE
[/js]

10. Suggest one! cheeky?
山姆·德灵

Sam Deering

Sam Deering has 15+ years of programming and website development experience. He was a website consultant at Console, ABC News, Flight Centre, Sapient Nitro, and the QLD Government and runs a tech blog with over 1 million views per month. Currently, Sam is the Founder of Crypto News, Australia.

New books out now!

💁‍♀️ Fun Fact: Python was ranked #2 on a recent highest paid coders list. *

Can I learn Python?

🤓 Ok. When did a code editor from Microsoft become kinda cool!?

Strange but true!

Popular Books

视觉工作室代码端到端针对Web开发人员的编辑和调试工具

Visual Studio Code: End-to-End Editing and Debugging Tools for Web Developers

远程工作的艺术

The Art of Working Remotely

表单设计模式

Form Design Patterns

From: https://www.sitepoint.com/10-cheeky-jquery-snippets/



推荐阅读
author-avatar
群-众-生-活
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有