Things It Might Be Fun/Useful to Try the Universal (*) Selector On

本文探讨了如何使用CSS的通用选择器(*)为页面上的所有元素设置样式属性,包括过渡效果、背景重复、定位方式及垂直居中等,旨在通过简化CSS规则提升用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Paul Irish recently wrote a post on using the universal selector to set border-box box-sizing on everything:

* {
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
}

I've been wanting to try that for a while and I'm happy to say I've used it on a few projects already and it's awesome. It also got my thinking what other properties might be similarly useful in applying to all elements on the page.

Transitions

* {
  -webkit-transition: all 0.2s ease;
  -moz-transition:    all 0.2s ease;
  -ms-transition:     all 0.2s ease;
  -o-transition:      all 0.2s ease;
}

If you're on a desktop browser, try it out right now. Open your browsers dev tools and add a new CSS rule. I happen to be in Chrome so:

I just tried in in the WordPress dashboard where I'm writing this and it's pretty wacky fun, especially in the side navigation. Performance could certainly be a concern here, so I'm not exactly advocating it's use right now, but it's fun to play with. When everything on the page has a quick and equal transition, I find it a nice, softened, comforting feel.

Non-Repeating Backgrounds

I bet overall you tell backgrounds to no-repeat more often than you actually leave them repeating.

* {
  background-repeat: no-repeat;
}

That way you can do:

.logo {
  background-image: url(logo.png);
}

Without worrying that the image you've set will repeat and be weird if the container is slightly bigger than the logo. And you also don't have to use the shorthand for background, which sets/resets background-color,background-attachment, and background-position whether you want it to or not.

I know Estelle Weyl is a fan =)

Relative Positioning

* {
  position: relative;
}

If everything starts out with relative positioning, that means z-index "just works" instead of the confusing issue where default statically positioned elements ignore it. It also means it's easier to nudge things around with top,leftright, and bottom which all "just work" on relatively positioned elements. It might be hard to apply this to an existing layout but starting from scratch with this shouldn't be too hard.

Middle Alignment

* {
  vertical-align: middle;
}

I find myself setting this value a lot, espeically on projects that use icons. It doesn't affect much on most layouts I tried applying it on, as it only really comes up when inline or inline-block elements line up with each other on the same line. Essentially, I find myself setting this more than resetting a default, which I think makes it a good candidate for universal settage.

Things It Might Be Fun/Useful to Try the Universal (*) Selector On is a post from CSS-Tricks


/*! * jQuery JavaScript Library v1.12.4 * http://jquery.com/ * * Includes Sizzle.js * http://sizzlejs.com/ * * Copyright jQuery Foundation and other contributors * Released under the MIT license * http://jquery.org/license * * Date: 2016-05-20T17:17Z */ (function( global, factory ) { if ( typeof module === "object" && typeof module.exports === "object" ) { // For CommonJS and CommonJS-like environments where a proper `window` // is present, execute the factory and get jQuery. // For environments that do not have a `window` with a `document` // (such as Node.js), expose a factory as module.exports. // This accentuates the need for the creation of a real `window`. // e.g. var jQuery = require("jquery")(window); // See ticket #14549 for more info. module.exports = global.document ? factory( global, true ) : function( w ) { if ( !w.document ) { throw new Error( "jQuery requires a window with a document" ); } return factory( w ); }; } else { factory( global ); } // Pass this if window is not defined yet }(typeof window !== "undefined" ? window : this, function( window, noGlobal ) { // Support: Firefox 18+ // Can't be in strict mode, several libs including ASP.NET trace // the stack via arguments.caller.callee and Firefox dies if // you try to trace through "use strict" call chains. (#13335) //"use strict"; var deletedIds = []; var document = window.document; var slice = deletedIds.slice; var concat = deletedIds.concat; var push = deletedIds.push; var indexOf = deletedIds.indexOf; var class2type = {}; var toString = class2type.toString; var hasOwn = class2type.hasOwnProperty; var support = {}; var version = "1.12.4", // Define a local copy of jQuery jQuery = function( selector, context ) { // The jQuery object is actually just the init constructor 'enhanced' // Need init if jQuery is called (just allow error to be thrown if not included) return new jQuery.fn.init( selector, context ); }
03-26
<think>好的,我现在需要处理用户关于jQuery 1.12.4源码解析和实现原理的问题。首先,用户希望了解jQuery的内部工作机制,所以我要先回忆jQuery的核心设计,比如选择器引擎、DOM操作、事件系统、Ajax封装以及插件机制。根据用户提供的引用,特别是引用4提到了$函数的构造和原型链,这可能是一个重点。 接下来,我需要整理jQuery的模块化结构。比如,入口模块如何初始化,工具方法如何实现,比如$.extend。然后,选择器引擎部分,用户提到的引用1和2都涉及选择器和库的特性,这里需要解释Sizzle引擎的工作原理,以及jQuery如何链式调用。 DOM操作部分,用户可能对封装后的方法如html()、append()如何工作感兴趣。事件系统方面,jQuery的事件委托和兼容性处理是关键点,引用5提到了动画,可能涉及队列管理。 Ajax模块的封装和Deferred异步处理也是用户可能想了解的,需要说明jQuery如何简化Ajax调用。插件机制方面,引用2提到插件丰富,所以需要解释如何扩展jQuery的原型或静态方法。 另外,用户的问题需要结合提供的引用内容,比如引用3和4关于压缩版源码和$函数的实现。需要确保回答结构清晰,分模块讲解,并附上示例代码帮助理解。最后生成的相关问题要覆盖源码结构、设计模式、选择器优化、事件委托和插件开发,这些都是用户可能进一步询问的方向。 在回答时,要遵循系统指令,正确使用LaTeX格式,确保行内公式用$...$,独立公式用$$...$$,但考虑到jQuery源码解析可能涉及较少数学公式,需要检查是否有必要使用。此外,引用标识要自然地添加在引用段落末尾,例如引用[^2]对应jQuery的宗旨。 需要验证引用的正确性,确保每个引用标记对应正确的来源。例如,引用4详细说明了$函数的构造,这点在回答中需要准确反映。同时,用户提供的引用中有一些是关于基本用法和动画的,但用户的问题更关注源码和原理,所以可能需要侧重引用3、4的内容。 最后,确保回答用中文,代码块正确包裹,语法正确,相关问题相关且具有针对性。可能还需要查阅jQuery 1.12.4的源码结构,确保描述的模块划分准确,例如入口模块、工具方法、选择器引擎等部分是否正确。此外,示例代码需要简洁,比如展示如何扩展插件,或链式调用的实现方式。 总结来说,回答需要分模块解析jQuery源码,结合用户提供的引用内容,尤其是关于$函数和原型链的部分,同时确保结构清晰,示例恰当,并生成相关的问题引导用户深入探索。</think># jQuery 1.12.4 源码解析与实现原理 ## 一、核心设计思想 jQuery 的核心目标是**简化 DOM 操作**,其设计基于以下原则: 1. **链式调用**:通过返回 `jQuery` 对象实现连续操作,例如 `$('div').addClass('box').show()` 2. **隐式迭代**:自动遍历匹配的 DOM 元素集合 3. **兼容性处理**:统一不同浏览器的 API 差异,如事件绑定机制[^2] ## 二、源码模块划分 ```text jQuery 1.12.4 源码结构 ├── 入口模块(core.js) # 初始化$函数和jQuery对象 ├── 工具方法(util.js) # 实现$.extend、$.each等方法 ├── 选择器引擎(sizzle.js) # CSS选择器解析与匹配 ├── DOM操作模块(manipulation.js)# 处理html()/append()等方法 ├── 事件系统(event.js) # 实现on()/trigger()等 ├── Ajax模块(ajax.js) # 封装XMLHttpRequest ├── 动画模块(effects.js) # 基础动画实现[^5] ``` ## 三、核心实现细节 ### 1. $函数构造过程 ```javascript var jQuery = function(selector, context) { return new jQuery.fn.init(selector, context); }; jQuery.fn = jQuery.prototype = { constructor: jQuery, // 其他原型方法... }; init.prototype = jQuery.fn; // 关键原型链连接[^4] ``` ### 2. 选择器引擎实现 - 使用 `Sizzle` 引擎解析 CSS 选择器 - 优先使用 `querySelectorAll` 等原生方法 - 通过 `pushStack` 方法维护操作链 ### 3. 事件系统 ```javascript // 事件委托实现示例 $.fn.on = function(types, selector, data, fn) { return this.each(function() { jQuery.event.add(this, types, fn, data, selector); }); }; ``` 事件处理流程: 1. 创建事件回调缓存 2. 处理事件命名空间 3. 绑定原生事件监听器 4. 实现事件冒泡代理 ## 四、插件机制实现 通过扩展 `jQuery.fn` 对象实现插件: ```javascript $.fn.myPlugin = function(options) { return this.each(function() { // 插件逻辑 }); }; // 调用方式:$('#box').myPlugin(); ``` ## 五、性能优化设计 1. **原型方法共享**:所有 jQuery 对象共享同一原型 2. **方法缓存**:对常用方法(如 `document.getElementById`)进行缓存 3. **延迟解析**:直到实际需要时才执行选择器匹配
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值