上篇问题

在上篇《iview源码解析(1)》中的index.js 入口文件的源码中有一段代码有点疑惑:

/**
* 在浏览器环境下默认加载组件
*/
// auto install
if (typeof window !== 'undefined' && window.Vue) {
install(window.Vue);
}

在引用 iview 组件的时候需要

Vue.use(iView, { locale });

注册组件,即使不执行 use 也把组件注册了,这两段代码不是有重复功能?这么处理的目的是为什么呢?是处理兼容性问题么?有木有大神指点下。

button 组件

button的核心样式代码是在mixins中,mixins的意思是混入在vue官网上对混入的解释是这样解释

混入 (mixins) 是一种分发 Vue 组件中可复用功能的非常灵活的方式。混入对象可以包含任意组件选项。当组件使用混入对象时,所有混入对象的选项将被混入该组件本身的选项。

也就是组件下在细分共享对象来混入。

问题

那在样式当中其他组件的样式也有可能混入到按钮里面的样式函数?但个人感觉应该是很少在其他组件当中用到吧?可能是我对整个iview的库整体不熟悉,有木有大神指点下。

我们在来看下 mixins 目录下 button.less的源码:


/**函数
* 设置按钮的内边距、字体大小、边框曲线
* @param @padding
* @param @font-size
* @param @border-radius
*/
.button-size(@padding; @font-size; @border-radius) {
padding: @padding;
font-size: @font-size;
border-radius: @border-radius;
}
/**函数
* 设置按钮的跟颜色有关的属性:字体颜色、背景颜色、边框颜色、以及子a标签的颜色
* @param @color
* @param @background
* @param @border
*/
.button-color(@color; @background; @border) {
color: @color;
background-color: @background;
border-color: @border;
// a inside Button which only work in Chrome
// http://stackoverflow.com/a/17253457
> a:only-child {
color: currentColor;
&:after {
content: '';
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
background: transparent;
}
}
}
/**函数
* 设置按钮的跟颜色有关的属性包括hover、active、disabled颜色变动
* @param @color
* @param @background
* @param @border
*/
.button-variant(@color; @background; @border) {
.button-color(@color; @background; @border);
//按钮伪类颜色设置
&:hover
//&:focus
{
.button-color(tint(@color, 20%); tint(@background, 20%); tint(@border, 20%));
}
&:active,
&.active {
.button-color(shade(@color, 5%); shade(@background, 5%); shade(@background, 5%));
}
//禁用按钮的颜色设置
&.disabled,
&[disabled],
fieldset[disabled] & {
&,
&:hover,
&:focus,
&:active,
&.active {
.button-color(@btn-disable-color; @btn-disable-bg; @btn-disable-border);
}
}
}
/**函数
* 按钮主样式
*/
.btn() {
display: inline-block;
margin-bottom: 0;
font-weight: @btn-font-weight;
text-align: center;
vertical-align: middle;
/**
*用于指定某个给定的区域是否允许用户操作,以及如何响应用户操作
*auto:当触控事件发生在元素上时,由浏览器来决定进行哪些操作,比如对viewport进行平滑、缩放等。
*none:当触控事件发生在元素上时,不进行任何操作。
*/
touch-action: manipulation;
cursor: pointer;
background-image: none;
border: 1px solid transparent;
white-space: nowrap;
line-height: @line-height-base;
user-select: none;
.button-size(@btn-padding-base; @btn-font-size; @btn-border-radius);
//transform: translate3d(0, 0, 0);
//transition: all @transition-time linear;
transition: color @transition-time linear, background-color @transition-time linear, border @transition-time linear, box-shadow @transition-time linear; > .@{css-prefix-iconfont} {
line-height: 1;
}
//按钮的伪类样式
&,
&:active,
&:focus {
outline: 0;
} &:not([disabled]):hover {
text-decoration: none;
} &:not([disabled]):active {
outline: 0;
// transition: none; // 如果不注释此行,那么active会和focus同时触发,此时focus的开始动画transition会无效
}
//禁用按钮样式
&.disabled,
&[disabled] {
cursor: @cursor-disabled;
> * {
pointer-events: none;
}
}
//设置大按钮
&-large {
.button-size(@btn-padding-large; @btn-font-size-large; @btn-border-radius);
}
//设置小按钮
&-small {
.button-size(@btn-padding-small; @btn-font-size; @btn-border-radius-small);
}
}
/** 不同类型按钮函数控制颜色
* 默认按钮、主按键、幽灵按钮、虚线按钮、文字按钮
*/
// Default
.btn-default() {
.button-variant(@btn-default-color; @btn-default-bg; @btn-default-border); &:hover
//&:focus
{
.button-color(tint(@primary-color, 20%); white; tint(@primary-color, 20%));
}
&:active,
&.active {
.button-color(shade(@primary-color, 5%); white; shade(@primary-color, 5%));
}
.active-btn-color(@primary-color);
} // Primary
.btn-primary() {
.button-variant(@btn-primary-color; @btn-primary-bg; @primary-color); &:hover,
//&:focus,
&:active,
&.active {
color: @btn-primary-color;
}
.active-btn-color(@primary-color);
} // Ghost
.btn-ghost() {
.button-variant(@btn-ghost-color, @btn-ghost-bg, @btn-ghost-border); &:hover
//&:focus
{
.button-color(tint(@primary-color, 20%); @btn-ghost-bg; tint(@primary-color, 20%));
}
&:active,
&.active {
.button-color(shade(@primary-color, 5%); @btn-ghost-bg; shade(@primary-color, 5%));
}
.active-btn-color(@primary-color);
} // Dashed
.btn-dashed() {
.button-variant(@btn-ghost-color, @btn-ghost-bg, @btn-ghost-border);
border-style: dashed; &:hover
//&:focus
{
.button-color(tint(@primary-color, 20%); @btn-ghost-bg; tint(@primary-color, 20%));
}
&:active,
&.active {
.button-color(shade(@primary-color, 5%); @btn-ghost-bg; shade(@primary-color, 5%));
}
.active-btn-color(@primary-color);
} // Text
.btn-text() {
.button-variant(@btn-ghost-color, @btn-ghost-bg, transparent); // for disabled
&.disabled,
&[disabled],
fieldset[disabled] & {
&,
&:hover,
&:focus,
&:active,
&.active {
.button-color(@btn-disable-color; @btn-ghost-bg; transparent);
}
} &:hover
//&:focus
{
.button-color(tint(@primary-color, 20%); @btn-ghost-bg; transparent);
}
&:active,
&.active {
.button-color(shade(@primary-color, 5%); @btn-ghost-bg; transparent);
}
.active-btn-color(@primary-color);
}

知识点(这里面列出来的知识点是我自己不是很熟悉的列出来)

1. color: currentColor

css3的扩展关键字,currentColor是 color 属性的值,具体意思是指:currentColor关键字的使用值是 color 属性值的计算值。

如果currentColor关键字被应用在 color 属性自身,则相当于是 color: inherit。

2. background: transparent

设置背透明

3. tint(color,weight)

less中的方法,它用于混合颜色与白色,它有以下参数:

color :它代表一个颜色对象。

weight :这是一个可选参数,通过在颜色和白色之间提供百分比平衡点来指定元素的权重。

4. shade(color,weight)

less中的方法,它用于混合颜色与黑色,它有以下参数:

color :它代表一个颜色对象。

weight :这是一个可选参数,通过在颜色和白色之间提供百分比平衡点来指定元素的权重。

5. touch-action: manipulation

用于指定某个给定的区域是否允许用户操作,以及如何响应用户操作

auto:当触控事件发生在元素上时,由浏览器来决定进行哪些操作,比如对viewport进行平滑、缩放等。

none:当触控事件发生在元素上时,不进行任何操作。

6. user-select: none

css3新增属性,值:

none:文本不能被选择。

text:可以选择文本。

all:当所有内容作为一个整体时可以被选择。如果双击或者在上下文上点击子元素,那么被选择的部分将是以该子元素向上回溯的最高祖先元素。

element:可以选择文本,但选择范围受元素边界的约束。

7. outline: 0

outline (轮廓)是绘制于元素周围的一条线,位于边框边缘的外围,可起到突出元素的作用。在谷歌浏览器中激活状态默认有轮廓线,这个可以去除那个轮廓线。

8. pointer-events: none;

css3新增属性,值

auto:与pointer-events属性未指定时的表现效果相同。在svg内容上与visiblepainted值相同。

none:元素永远不会成为鼠标事件的target。但是,当其后代元素的pointer-events属性指定其他值时,鼠标事件可以指向后代元素,在这种情况下,鼠标事件将在捕获或冒泡阶触发父元素的事件侦听器。

9. less中把很多通用的属性值赋值到一个变量中。

在custom.less中如:按钮的基础变量

// Button
@btn-font-weight : normal;
@btn-padding-base : 6px 15px;
@btn-padding-large : 6px 15px 7px 15px;
@btn-padding-small : 2px 7px;
@btn-font-size : 12px;
@btn-font-size-large : 14px;
@btn-border-radius : 4px;
@btn-border-radius-small: 3px;
@btn-group-border : shade(@primary-color, 5%);

10. 里面将样式安装功能拆分成函数相互调用。

11. &代表的上一层选择器的名字。

vue UI库iview源码解析(2)的更多相关文章

  1. iview源码解析(1)

    概述 公司技术栈开始用vue主导开发,但因为公司前端会vue的不多所以在项目中用到vue的技术不是很深,之前出去面试被接连打击,而且本来打算开始为公司vue的项目构建自己的组件库所以去下载了iview ...

  2. Vue.js 2.0源码解析之前端渲染篇

    一.前言 Vue.js框架是目前比较火的MVVM框架之一,简单易上手的学习曲线,友好的官方文档,配套的构建工具,让Vue.js在2016大放异彩,大有赶超React之势.前不久Vue.js 2.0正式 ...

  3. elementUi源码解析(1)--项目结构篇

    因为在忙其他事情好久没有更新iview的源码,也是因为后面的一些组件有点复杂在考虑用什么方式把复杂的功能逻辑简单的展示出来,还没想到方法,突然想到element的组件基本也差不多,内部功能的逻辑也差不 ...

  4. Android 全面插件化 RePlugin 流程与源码解析

    转自 Android 全面插件化 RePlugin 流程与源码解析 RePlugin,360开源的全面插件化框架,按照官网说的,其目的是“尽可能多的让模块变成插件”,并在很稳定的前提下,尽可能像开发普 ...

  5. 【vuejs深入二】vue源码解析之一,基础源码结构和htmlParse解析器

    写在前面 一个好的架构需要经过血与火的历练,一个好的工程师需要经过无数项目的摧残. vuejs是一个优秀的前端mvvm框架,它的易用性和渐进式的理念可以使每一个前端开发人员感到舒服,感到easy.它内 ...

  6. Vue源码解析之nextTick

    Vue源码解析之nextTick 前言 nextTick是Vue的一个核心功能,在Vue内部实现中也经常用到nextTick.但是,很多新手不理解nextTick的原理,甚至不清楚nextTick的作 ...

  7. andorid jar/库源码解析之Bolts

    目录:andorid jar/库源码解析 Bolts: 作用: 用于链式执行跨线程代码,且传递数据 栗子: Task.call(new Callable<Boolean>() { @Ove ...

  8. andorid jar/库源码解析之Dagger/Dagger2

    目录:andorid jar/库源码解析 Dagger.Dagger2: 作用: 1.用于解耦Activity和业务逻辑 2.在使用业务的时候,不需要重复编写new代码. 3.当业务变化的时候,不需要 ...

  9. 【vuejs深入三】vue源码解析之二 htmlParse解析器的实现

    写在前面 一个好的架构需要经过血与火的历练,一个好的工程师需要经过无数项目的摧残. 昨天博主分析了一下在vue中,最为基础核心的api,parse函数,它的作用是将vue的模板字符串转换成ast,从而 ...

随机推荐

  1. Mybatis事务(二)事务隔离级别

    一般数据库的隔离级别有4个,由低到高依次为Read uncommitted.Read committed.Repeatable read.Serializable,这四个级别可以逐个解决脏读.不可重复 ...

  2. ROS(indigo)_turtlebot仿真示例包括stage和gazebo

    ROS(indigo)_turtlebot仿真示例包括stage和gazebo 现上参考网址: turtlebot:http://wiki.ros.org/Robots/TurtleBot stage ...

  3. 【转载】2015 Objective-C 三大新特性 | 干货

    Overview 自 WWDC 2015 推出和开源 Swift 2.0 后,大家对 Swift 的热情又一次高涨起来,在羡慕创业公司的朋友们大谈 Swift 新特性的同时,也有很多像我一样工作上依然 ...

  4. Dynamics CRM2011/2013 站点地图sitemap的翻译

    实体.属性字段.ribbon等的翻译可以通过解决方案来解决(具体可见我前面的博客:http://blog.csdn.net/vic0228/article/details/37690913),但解决方 ...

  5. Socket编程实践(6) --TCP服务端注意事项

    僵尸进程处理 1)通过忽略SIGCHLD信号,避免僵尸进程 在server端代码中添加 signal(SIGCHLD, SIG_IGN); 2)通过wait/waitpid方法,解决僵尸进程 sign ...

  6. 【leetcode】经典算法题-Counting Bits

    题目描述: 给定一个数字n,统计0-n之间的数字二进制的1的个数,并用数组输出 例子: For num = 5 you should return [0,1,1,2,1,2]. 要求: 算法复杂复o( ...

  7. MinerUtil.java 爬虫工具类

    MinerUtil.java 爬虫工具类 package com.iteye.injavawetrust.miner; import java.io.File; import java.io.File ...

  8. MinerConstanits.java 常量类

    MinerConstanits.java 常量类 package com.iteye.injavawetrust.miner; /** * 常量类 * @author InJavaWeTrust * ...

  9. leetcode 104 Maximum Depth of Binary Tree二叉树求深度

    Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question ...

  10. 认识一下Android 事件分发机制

    1.引子 由于android是采用分层布局(可以想象成PS时的图层概念一样),这样才可以在有限大小的手机屏幕上完成一些复杂的操作.当手指点击屏幕开始,这些动作在各层之间如何传递?就引出了Android ...