$(document).ready(function() {function equalHeight(group) {
tallest = 0; group.each(function() { thisHeight = $(this).height(); if(thisHeight > tallest) { tallest = thisHeight; } }); group.height(tallest);}// how to use$(document).ready(function() { equalHeight($(".left")); equalHeight($(".right"));});});
8. 动态控制页面字体大小
用户可以改变页面字体大小
?
$(document).ready(function() {
// Reset the font size(back to default) var originalFontSize = $('html').css('font-size'); $(".resetFont").click(function(){ $('html').css('font-size', originalFontSize); }); // Increase the font size(bigger font0 $(".increaseFont").click(function(){ var currentFontSize = $('html').css('font-size'); var currentFontSizeNum = parseFloat(currentFontSize, 10); var newFontSize = currentFontSizeNum*1.2; $('html').css('font-size', newFontSize); return false; }); // Decrease the font size(smaller font) $(".decreaseFont").click(function(){ var currentFontSize = $('html').css('font-size'); var currentFontSizeNum = parseFloat(currentFontSize, 10); var newFontSize = currentFontSizeNum*0.8; $('html').css('font-size', newFontSize); return false; });});
9. 返回页面顶部功能
For a smooth(animated) ride back to the top(or any location).
?
$(document).ready(function() {
$('a[href*=#]').click(function() { if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) { var $target = $(this.hash); $target = $target.length && $target || $('[name=' + this.hash.slice(1) +']'); if ($target.length) { var targetOffset = $target.offset().top; $('html,body') .animate({scrollTop: targetOffset}, 900); return false; } } });// how to use// place this where you want to scroll to<A name=top></A>// the link<A href="#top">go to top</A>});
10. 获得鼠标指针XY值
Want to know where your mouse cursor is?
?
$(document).ready(function() {
$().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); });// how to use<DIV id=XY></DIV>});
11.返回顶部按钮
你可以利用 animate 和 scrollTop 来实现返回顶部的动画,而不需要使用其他插件。
?
// Back to top
$('a.top').click(function () { $(document.body).animate({scrollTop: 0}, 800); return false;});<!-- Create an anchor tag --><a class="top" href="#">Back to top</a>
改变 scrollTop 的值可以调整返回距离顶部的距离,而 animate 的第二个参数是执行返回动作需要的时间(单位:毫秒)。
12.预加载图片
如果你的页面中使用了很多不可见的图片(如:hover 显示),你可能需要预加载它们:
?