less的运用

目录

一、变量 

二、混合

1. 混合写法:

2. 混合带参数:

3. 混合带参数,参数可以带默认值

三、匹配模式

四、嵌套

五、运算

六、逻辑函数

七、字符串函数

1. escape()

2. e()

3. %()

4.  replace()


官网地址:Less 函数 | Less.js 中文文档 - Less 中文网

先引入文件:

 <!-- 注意less文件一定要在js的文件上面 -->

    <link rel="stylesheet/less" type="text/css" href="./css/1.less" />

    <script src="https://cdn.jsdelivr.net/npm/less@4" ></script>

一、变量 

// 定义变量
@w:100px;
//定义属性名,用的时候要加一个大括号
@aa:color;
//定义路径变量必须加引号,用的时候要加一个大括号
@images:'../images';
.box{
    width: @w+100;
    height: @w;
    background-color: pink;
    @{aa}: green;
    background-image: url('@{images}/02.jpg');
}

二、混合

1. 混合写法:

把一个选择器放入另一个选择器样式里,第二个就具有第一个的样式

.box1{
    font: 20px/40px '微软雅黑'; //大小20,行高40
    text-align: center;
    background: green;
    margin: 2px;
}

//混合写法:把另一个选择器名放在这个样式里,这个样式就具有放入选择器的样式
.box2{
    width: @width;
    height: @width;
    .box1();  //可以加括号,可以省略
    .bg(pink)
}

2. 混合带参数:

//混合带参数
.bg(@a){
    background-color: @a;
}
.box3{
    width: @width+50;
    height: @width+50;
    .bg(blue)
}

3. 混合带参数,参数可以带默认值

注意:调用时不带实参,使用默认值。带实参,传递实参。

//混合带参数,参数可以带默认值
.border(@b:10px){
    border: @b solid green;
}
.box4{
    height: @width+20;
    width: @width+20;
    .border(15px)   //不带实参,使用默认值。带实参,传递实参。
}


.border2(@b:5px,@style:solid,@color:black){
    border: @b @style @color;
}
.box5{
    height: @width+50;
    width: @width+50;
    // .border2(@b:10px);   // 写法一
    .border2(10px,dotted,red)   //写法二
}

//兼容
.boxShadow(@x:5px,@y:5px,@area:5px,@color:#ccc){
    -webkit-box-shadow: @x @y @area @color;  //谷歌
    -moz-box-shadow: @x @y @area @color;     //火狐
    -ms-box-shadow: @x @y @area @color;     //IE
    -o-box-shadow: @x @y @area @color;     // opera
    box-shadow: @x @y @area @color; 
}
.box6{
    .boxShadow();
    height: @width+50;
    width: @width+50;
}

三、匹配模式

注意:匹配模式中的第一个值是 自定义的

注意:公用样式 第一个参数是固定的格式【@_】,后面的参数与上面保持一致

.triangle(top,@w:5px,@c:red){
    border-width: @w; //边框大小
    border-color: transparent transparent @c transparent;
    border-style: dashed dashed solid dashed;
}
.triangle(right,@w:5px,@c:red){
    border-width: @w; //边框大小
    border-color: transparent transparent transparent @c;
    border-style: dashed dashed dashed solid;
}

//公用样式,第一个参数是固定的格式【@_】,后面的参数与上面保持一致
.triangle(@_,@w:5px,@c:red){
    width: 0;
    height: 0;
    overflow: hidden;
}

.box2{
    .triangle(top)
}

四、嵌套

注意:& :是上一层选择器

.box {
    width: 500px;
    padding: 20px;
    border: 1px solid rgb(255, 0, 0);
    &{  // & : 上一层选择器
        border: 5px solid rebeccapurple;
    }
    h2 {
        font-size: 20px/20px '微软雅黑';
    }
    ul {
        margin: 0;
        padding: 0;
        list-style: none;
        li {
            height: 30px;
            background-color: #ccc;
            a {
                color: #f00;
                &:hover{  // & : 上一层选择器
                    color: pink;
                }
            }
        }
    }
}

五、运算

注意:less到4版本后,使用减法,需要在 - 号两边加上空格

注意:多次运算也要加括号

~" ":转义(Escaping)允许你使用任意字符串作为属性或变量值。任何 ~"anything" 或 ~'anything' 形式的内容都将按原样输出。不编译

@w:100px;
@q:200px;

.box{
    width: @w+100;
    height: @w - 50;  // 注意使用减法 需要在两边加上空格
    border: 1px solid saddlebrown;
    position: relative;
    left: @w*2;
    top: (@w/5);  //注意:less到4版本后,必须要加括号。或者在/前加点(./)
    margin: ((@q/2) / 2);  //注意:多次运算也要加括号
    //编码编译,在内容前加【~'内容'】
    filter: ~'alpha(opacity:60)'; //在IE中设置透明
}

六、逻辑函数

//if
div{
        width: (@q / 2); //100
        height: (@q / 2);
        background-color: if((2>1),blue,pink);
    }

//布尔值
@bg:boolean(3<2);
.box3{
    width: @q;
    height: @q;
    background-color: if(@bg,red,black);
}

七、字符串函数

1. escape()

escape()函数:将字符串中 url 特殊字符进行编码处理

如下特殊字符:#   ^   ( )  { }   |   :   >  <   ;   [ ]   =

转义编码有: , / ? @ & + ' ~ ! $

例如:

// less中
.box{
    height:escape('123=abc');
}
/*css中*/
.box {
  height: 123%3Dabc;
}

2. e()

原样输出内容,避免编译。css转义,也可用  ~' '

//less中
.box2{
    width: e('1200px');
    height: ~'200px';
}
/*css中*/
.box2 {
  width: 1200px;
  height: 200px;
}

3. %()

格式化一个字符串

补充:less中占位符分为:a、A、d、D、s、S
a、A、d、D能被任何类型参数替换(颜色值、数字、转义值、表达式…),如果是在字符串中结合使用,整个字符串都会替换进去,包括它的引号,然后,引号会被替换到字符串参数的原来位置,可能被转义或者没有被转义,这取决于占位符是大写字母还是小写字母

s、S能被除了颜色值以外的任何类型的参数替换,如果在字符串中结合使用,只会替换成字符串参数的值,而字符串参数引号被忽略

//less
.box3{
    height: 100px;
    width: 100px;
    background-color: %('%a',pink);
    font-family: %('%a %a',"思源宋体","思源黑体");
}
/*css中*/
.box3 {
  height: 100px;
  width: 100px;
  background-color: '#ffc0cb';
  font-family: '"思源宋体" "思源黑体"';
}

4.  replace()

参数:

  • string:要搜索和替换的字符串。
  • pattern:要搜索的字符串或正则表达式模式。
  • replacement: 替换匹配模式的字符串。
  • flags:(可选)正则表达式标志。

返回: 具有替换值的字符串。

//less
.box3::after{
    content: replace("hello,mars?", "mars?", "dui");
}
//css
.box3::after {
  content: "hello,dui?";
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以给您提供一个简单的例子,但是需要您提供更具体的要求和细节。以下是一个可能的例子: 题目:实现一个解密程序,将一个加密的字符串解密成明文。 要求: 1. 加密方式:将明文的每个字符替换成它的ASCII码值加上一个密钥(密钥为一个小于256的正整数),得到一个新的ASCII码值,再将新的ASCII码值转换成一个字符。 2. 解密方式:将密文的每个字符替换成它的ASCII码值减去密钥,得到明文的ASCII码值,再将ASCII码值转换成一个字符。 提示: 1. 要求用户输入一个密钥和一个加密的字符串。 2. 对于输入的密钥,需要进行检查以确保它是一个小于256的正整数。 3. 对于输入的加密字符串,需要进行检查以确保它不包含非ASCII字符。 以下是一个可能的Python代码示例(大约200行): ```python def encrypt(plaintext, key): ciphertext = '' for char in plaintext: if ord(char) > 255: raise ValueError('Input string contains non-ASCII characters') ciphertext += chr((ord(char) + key) % 256) return ciphertext def decrypt(ciphertext, key): plaintext = '' for char in ciphertext: plaintext += chr((ord(char) - key) % 256) return plaintext def main(): key = int(input('Please enter a key (a positive integer less than 256): ')) if key < 1 or key >= 256: raise ValueError('Invalid key') ciphertext = input('Please enter the encrypted string: ') plaintext = decrypt(ciphertext, key) print('The decrypted string is:', plaintext) if __name__ == '__main__': main() ``` 这个程序使用了一些基本的离散数学概念,例如ASCII码和模运算。希望对您有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值