shell对文件的操作

shell脚本编写中,时常会用到对文件的相关操作,比如增加内容,修改内容,删除部分内容,查看部分内容等,但是上述举例的这些操作一般都是需要在文本编辑器中才能操作,常用的文本编辑器如:gedit、vim、nano等又是交互式文本编辑器,脚本无法自己独立完成,必须有人参与才可以完成。如果这样的话又违背了我们编写脚本的意愿(全部由机器来完成,减少人的工作压力,提升工作效率)。emm…如何才能让这些操作全部脚本自己就搞定,而不需要人的参与,而且又能按照我们的脚本预案来完成呢?

为了解决上述问题,linux为大家提供了一些命令,比如Perl、sed等命令,今天我就着重为大家介绍一下sed命令。

一、sed介绍

sed是linux中提供的一个外部命令,它是一个行(流)编辑器,非交互式的对文件内容进行增删改查的操作,使用者只能在命令行输入编辑命令、指定文件名,然后在屏幕上查看输出。它和文本编辑器有本质的区别

代码语言:javascript代码运行次数:0运行复制

区别是:文本编辑器: 编辑对象是文件行编辑器:编辑对象是文件中的行

也就是前者一次处理一个文本,而后者是一次处理一个文本中的一行。这个是我们应该弄清楚且必须牢记的,否者可能无法理解sed的运行原理和使用精髓。

sed数据处理原理

shell对文件的操作

二、sed语法

sed 命令语法:

sed [options] ‘{command}[flags]’ [filename]

代码语言:javascript代码运行次数:0运行复制

#命令选项-e script 将脚本中指定的命令添加到处理输入时执行的命令中  多条件,一行中要有多个操作-f script 将文件中指定的命令添加到处理输入时执行的命令中-n        抑制自动输出-i        编辑文件内容-i.bak    修改时同时创建.bak备份文件。-r        使用扩展的正则表达式!         取反 (跟在模式条件后与shell有所区别)#command   对文件干什么sed常用内部命令a   在匹配后面添加i   在匹配前面添加d   删除s   查找替换  字符串c   更改y   转换   N D P p   打印#flags数字             表示新文本替换的模式g:             表示用新文本替换现有文本的全部实例p:             表示打印原始的内容w filename:     将替换的结果写入文件

2.1)sed内部命令说明

演示实例文档

代码语言:javascript代码运行次数:0运行复制

[root@zutuanxue ~]# cat data11 the quick brown fox jumps over the lazy dog.2 the quick brown fox jumps over the lazy dog.3 the quick brown fox jumps over the lazy dog.4 the quick brown fox jumps over the lazy dog.5 the quick brown fox jumps over the lazy dog.

文件内容增加操作,将数据追加到某个位置之后,使用命令a。

演示案例

代码语言:javascript代码运行次数:0运行复制

在data1的每行后追加一行新数据内容: append data "haha"[root@zutuanxue ~]# sed 'aappend data "haha"' data11 the quick brown fox jumps over the lazy dog.append data "haha"2 the quick brown fox jumps over the lazy dog.append data "haha"3 the quick brown fox jumps over the lazy dog.append data "haha"4 the quick brown fox jumps over the lazy dog.append data "haha"5 the quick brown fox jumps over the lazy dog.append data "haha"在第二行后新开一行追加数据: append data "haha"[root@zutuanxue ~]# sed '2aappend data "haha"' data11 the quick brown fox jumps over the lazy dog.2 the quick brown fox jumps over the lazy dog.append data "haha"3 the quick brown fox jumps over the lazy dog.4 the quick brown fox jumps over the lazy dog.5 the quick brown fox jumps over the lazy dog.在第二到四行每行后新开一行追加数据: append data "haha"[root@zutuanxue ~]# sed '2,4aappend data "haha"' data11 the quick brown fox jumps over the lazy dog.2 the quick brown fox jumps over the lazy dog.append data "haha"3 the quick brown fox jumps over the lazy dog.append data "haha"4 the quick brown fox jumps over the lazy dog.append data "haha"5 the quick brown fox jumps over the lazy dog.匹配字符串追加: 找到包含"3 the"的行,在其后新开一行追加内容: append data "haha"[root@zutuanxue ~]# sed '/3 the/aappend data "haha"' data11 the quick brown fox jumps over the lazy dog.2 the quick brown fox jumps over the lazy dog.3 the quick brown fox jumps over the lazy dog.append data "haha"4 the quick brown fox jumps over the lazy dog.5 the quick brown fox jumps over the lazy dog.//开启匹配模式  /要匹配的字符串/

文件内容增加操作,将数据插入到某个位置之前,使用命令i。

演示案例

代码语言:javascript代码运行次数:0运行复制

在data1的每行前插入一行新数据内容: insert data "haha"[root@zutuanxue ~]# sed 'iinsert data "haha"' data1insert data "haha"1 the quick brown fox jumps over the lazy dog.insert data "haha"2 the quick brown fox jumps over the lazy dog.insert data "haha"3 the quick brown fox jumps over the lazy dog.insert data "haha"4 the quick brown fox jumps over the lazy dog.insert data "haha"5 the quick brown fox jumps over the lazy dog.在第二行前新开一行插入数据: insert data "haha"[root@zutuanxue ~]# sed '2iinsert data "haha"' data11 the quick brown fox jumps over the lazy dog.insert data "haha"2 the quick brown fox jumps over the lazy dog.3 the quick brown fox jumps over the lazy dog.4 the quick brown fox jumps over the lazy dog.5 the quick brown fox jumps over the lazy dog.在第二到四行每行前新开一行插入数据: insert data "haha"[root@zutuanxue ~]# sed '2,4iinsert data "haha"' data11 the quick brown fox jumps over the lazy dog.insert data "haha"2 the quick brown fox jumps over the lazy dog.insert data "haha"3 the quick brown fox jumps over the lazy dog.insert data "haha"4 the quick brown fox jumps over the lazy dog.5 the quick brown fox jumps over the lazy dog.匹配字符串插入: 找到包含"3 the"的行,在其前新开一行插入内容: insert data "haha"[root@zutuanxue ~]# sed '/3 the/iinsert data "haha"' data11 the quick brown fox jumps over the lazy dog.2 the quick brown fox jumps over the lazy dog.insert data "haha"3 the quick brown fox jumps over the lazy dog.4 the quick brown fox jumps over the lazy dog.5 the quick brown fox jumps over the lazy dog.

文件内容修改操作–替换,将一行中匹配的内容替换为新的数据,使用命令s。

演示案例

代码语言:javascript代码运行次数:0运行复制

从标准输出流中做替换,将test替换为text[root@zutuanxue ~]# echo "this is a test" |sed 's/test/text/'this is a text将data1中每行的dog替换为cat[root@zutuanxue ~]# sed 's/dog/cat/' data11 the quick brown fox jumps over the lazy cat.2 the quick brown fox jumps over the lazy cat.3 the quick brown fox jumps over the lazy cat.4 the quick brown fox jumps over the lazy cat.5 the quick brown fox jumps over the lazy cat.将data1中第二行的dog替换为cat[root@zutuanxue ~]# sed '2s/dog/cat/' data11 the quick brown fox jumps over the lazy dog.2 the quick brown fox jumps over the lazy cat.3 the quick brown fox jumps over the lazy dog.4 the quick brown fox jumps over the lazy dog.5 the quick brown fox jumps over the lazy dog.将data1中第二到第四行的dog替换为cat[root@zutuanxue ~]# sed '2,4s/dog/cat/' data11 the quick brown fox jumps over the lazy dog.2 the quick brown fox jumps over the lazy cat.3 the quick brown fox jumps over the lazy cat.4 the quick brown fox jumps over the lazy cat.5 the quick brown fox jumps over the lazy dog.匹配字符串替换:将包含字符串"3 the"的行中的dog替换为cat[root@zutuanxue ~]# sed '/3 the/s/dog/cat/' data11 the quick brown fox jumps over the lazy dog.2 the quick brown fox jumps over the lazy dog.3 the quick brown fox jumps over the lazy cat.4 the quick brown fox jumps over the lazy dog.5 the quick brown fox jumps over the lazy dog.

文件内容修改操作–更改,将一行中匹配的内容替换为新的数据,使用命令c。

演示案例

代码语言:javascript代码运行次数:0运行复制

将data1文件中的所有行的内容更改为: change data "data"[root@zutuanxue ~]# sed 'cchange data "haha"' data1change data "haha"change data "haha"change data "haha"change data "haha"change data "haha"将data1文件第二行的内容更改为: change data "haha"[root@zutuanxue ~]# sed '2cchange data "haha"' data11 the quick brown fox jumps over the lazy dog.change data "haha"3 the quick brown fox jumps over the lazy dog.4 the quick brown fox jumps over the lazy dog.5 the quick brown fox jumps over the lazy dog.将data1文件中的第二、三、四行的内容更改为:change data "haha"[root@zutuanxue ~]# sed '2,4cchange data "haha"' data11 the quick brown fox jumps over the lazy dog.change data "haha"5 the quick brown fox jumps over the lazy dog.将data1文件中包含"3 the"的行内容更改为: change data "haha"[root@zutuanxue ~]# sed '/3 the/cchange data "data"' data11 the quick brown fox jumps over the lazy dog.2 the quick brown fox jumps over the lazy dog.change data "data"4 the quick brown fox jumps over the lazy dog.5 the quick brown fox jumps over the lazy dog.

文件内容修改操作–字符转换,将一行中匹配的内容替换为新的数据,使用命令y。

演示案例

代码语言:javascript代码运行次数:0运行复制

将data1中的a b c字符转换为对应的 A  B  C字符[root@zutuanxue ~]# sed 'y/abc/ABC/' data11 the quiCk Brown fox jumps over the lAzy dog.2 the quiCk Brown fox jumps over the lAzy dog.3 the quiCk Brown fox jumps over the lAzy dog.4 the quiCk Brown fox jumps over the lAzy dog.5 the quiCk Brown fox jumps over the lAzy dog.

文件内容删除,将文件中的指定数据删除,使用命令d。

演示案例

代码语言:javascript代码运行次数:0运行复制

删除文件data1中的所有数据[root@zutuanxue ~]# sed 'd' data1删除文件data1中的第三行数据[root@zutuanxue ~]# sed '3d' data11 the quick brown fox jumps over the lazy dog.2 the quick brown fox jumps over the lazy dog.4 the quick brown fox jumps over the lazy dog.5 the quick brown fox jumps over the lazy dog.删除文件data1第三到第四行的数据[root@zutuanxue ~]# sed '3,4d' data11 the quick brown fox jumps over the lazy dog.2 the quick brown fox jumps over the lazy dog.5 the quick brown fox jumps over the lazy dog.删除文件data1中包含字符串"3 the"的行[root@zutuanxue ~]# sed '/3 the/d' data11 the quick brown fox jumps over the lazy dog.2 the quick brown fox jumps over the lazy dog.4 the quick brown fox jumps over the lazy dog.5 the quick brown fox jumps over the lazy dog.

文件内容查看,将文件内容输出到屏幕,使用命令p。

讯飞写作 讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

讯飞写作 56 查看详情 讯飞写作

演示案例

代码语言:javascript代码运行次数:0运行复制

打印data1文件内容[root@zutuanxue ~]# sed 'p' data11 the quick brown fox jumps over the lazy dog.1 the quick brown fox jumps over the lazy dog.2 the quick brown fox jumps over the lazy dog.2 the quick brown fox jumps over the lazy dog.3 the quick brown fox jumps over the lazy dog.3 the quick brown fox jumps over the lazy dog.4 the quick brown fox jumps over the lazy dog.4 the quick brown fox jumps over the lazy dog.5 the quick brown fox jumps over the lazy dog.5 the quick brown fox jumps over the lazy dog.打印data1文件第三行的内容[root@zutuanxue ~]# sed '3p' data11 the quick brown fox jumps over the lazy dog.2 the quick brown fox jumps over the lazy dog.3 the quick brown fox jumps over the lazy dog.3 the quick brown fox jumps over the lazy dog.4 the quick brown fox jumps over the lazy dog.5 the quick brown fox jumps over the lazy dog.打印data1文件第二、三、四行内容[root@zutuanxue ~]# sed '2,4p' data11 the quick brown fox jumps over the lazy dog.2 the quick brown fox jumps over the lazy dog.2 the quick brown fox jumps over the lazy dog.3 the quick brown fox jumps over the lazy dog.3 the quick brown fox jumps over the lazy dog.4 the quick brown fox jumps over the lazy dog.4 the quick brown fox jumps over the lazy dog.5 the quick brown fox jumps over the lazy dog.打印data1文件包含字符串"3 the"的行[root@zutuanxue ~]# sed '/3 the/p' data11 the quick brown fox jumps over the lazy dog.2 the quick brown fox jumps over the lazy dog.3 the quick brown fox jumps over the lazy dog.3 the quick brown fox jumps over the lazy dog.4 the quick brown fox jumps over the lazy dog.5 the quick brown fox jumps over the lazy dog. 可以看得出,打印内容是重复的行,原因是打印了指定文件内容一次,又将读入缓存的所有数据打印了一次,所以会看到这样的效果,如果不想看到这样的结果,可以加命令选项-n抑制内存输出即可。

2.2)命令选项说明

sed语法 在sed命令中,命令选项是对sed中的命令的增强

在命令行中使用多个命令 -e

代码语言:javascript代码运行次数:0运行复制

将brown替换为green dog替换为cat[root@zutuanxue ~]# sed -e 's/brown/green/;s/dog/cat/' data11 the quick green fox jumps over the lazy cat.2 the quick green fox jumps over the lazy cat.3 the quick green fox jumps over the lazy cat.4 the quick green fox jumps over the lazy cat.5 the quick green fox jumps over the lazy cat.

从文件读取编辑器命令 -f 适用于日常重复执行的场景

代码语言:javascript代码运行次数:0运行复制

1)将命令写入文件[root@zutuanxue ~]# vim abcs/brown/green/s/dog/cat/s/fox/elephant/2)使用-f命令选项调用命令文件[root@zutuanxue ~]# sed -f abc data1 1 the quick green elephant jumps over the lazy cat.2 the quick green elephant jumps over the lazy cat.3 the quick green elephant jumps over the lazy cat.4 the quick green elephant jumps over the lazy cat.5 the quick green elephant jumps over the lazy cat.

抑制内存输出 -n

代码语言:javascript代码运行次数:0运行复制

打印data1文件的第二行到最后一行内容  $最后的意思[root@zutuanxue ~]# sed -n '2,$p' data1 2 the quick brown fox jumps over the lazy dog.3 the quick brown fox jumps over the lazy dog.4 the quick brown fox jumps over the lazy dog.5 the quick brown fox jumps over the lazy dog.

使用正则表达式 -r

代码语言:javascript代码运行次数:0运行复制

打印data1中以字符串"3 the"开头的行内容[root@zutuanxue ~]# sed -n  -r '/^(3 the)/p' data13 the quick brown fox jumps over the lazy dog.

从上述的演示中,大家可以看出,数据处理只是在缓存中完成的,并没有实际修改文件内容,如果需要修改文件内容可以直接使用-i命令选项。在这里我需要说明的是-i是一个不可逆的操作,一旦修改,如果想复原就很困难,几乎不可能,所以建议大家在操作的时候可以备份一下源文件。-i命令选项提供了备份功能,比如参数使用-i.bak,那么在修改源文件的同时会先备份一个以.bak结尾的源文件,然后再进行修改操作。

代码语言:javascript代码运行次数:0运行复制

1)查看文件列表,没有发现data1.bak[root@zutuanxue ~]# lsabc  apache  data1  Dobby  file  node-v10.14.1  Python-3.7.1  soft1  vimset2)执行替换命令并修改文件[root@zutuanxue ~]# sed -i.bak 's/brown/green/' data13)发现文件夹中多了一个data1.bak文件[root@zutuanxue ~]# lsabc     data1      Dobby  node-v10.14.1  soft1apache  data1.bak  file   Python-3.7.1   vimset4)打印比较一下,发现data1已经被修改,data1.bak是源文件的备份。[root@zutuanxue ~]# cat data11 the quick green fox jumps over the lazy dog.2 the quick green fox jumps over the lazy dog.3 the quick green fox jumps over the lazy dog.4 the quick green fox jumps over the lazy dog.5 the quick green fox jumps over the lazy dog.[root@zutuanxue ~]# cat data1.bak 1 the quick brown fox jumps over the lazy dog.2 the quick brown fox jumps over the lazy dog.3 the quick brown fox jumps over the lazy dog.4 the quick brown fox jumps over the lazy dog.5 the quick brown fox jumps over the lazy dog.

2.3)标志

在sed命令中,标志是对sed中的内部命令做补充说明

代码语言:javascript代码运行次数:0运行复制

演示文档[root@zutuanxue ~]# cat data21 the quick brown fox jumps over the lazy dog . dog2 the quick brown fox jumps over the lazy dog . dog3 the quick brown fox jumps over the lazy dog . dog4 the quick brown fox jumps over the lazy dog . dog5 the quick brown fox jumps over the lazy dog . dog

数字标志:此标志是一个非零正数,默认情况下,执行替换的时候,如果一行中有多个符合的字符串,如果没有标志位定义,那么只会替换第一个字符串,其他的就被忽略掉了,为了能精确替换,可以使用数字位做定义。

代码语言:javascript代码运行次数:0运行复制

替换一行中的第二处dog为cat[root@zutuanxue ~]# sed 's/dog/cat/2' data21 the quick brown fox jumps over the lazy dog . cat2 the quick brown fox jumps over the lazy dog . cat3 the quick brown fox jumps over the lazy dog . cat4 the quick brown fox jumps over the lazy dog . cat5 the quick brown fox jumps over the lazy dog . cat

g标志:将一行中的所有符合的字符串全部执行替换

代码语言:javascript代码运行次数:0运行复制

将data1文件中的所有dog替换为cat[root@zutuanxue ~]# sed 's/dog/cat/g' data21 the quick brown fox jumps over the lazy cat . cat2 the quick brown fox jumps over the lazy cat . cat3 the quick brown fox jumps over the lazy cat . cat4 the quick brown fox jumps over the lazy cat . cat5 the quick brown fox jumps over the lazy cat . cat

p标志:打印文本内容,类似于-p命令选项

代码语言:javascript代码运行次数:0运行复制

[root@zutuanxue ~]# sed  '3s/dog/cat/p' data21 the quick brown fox jumps over the lazy dog . dog2 the quick brown fox jumps over the lazy dog . dog3 the quick brown fox jumps over the lazy cat . dog3 the quick brown fox jumps over the lazy cat . dog4 the quick brown fox jumps over the lazy dog . dog5 the quick brown fox jumps over the lazy dog . dog

w filename标志:将修改的内容存入filename文件中

代码语言:javascript代码运行次数:0运行复制

[root@zutuanxue ~]# sed  '3s/dog/cat/w text' data21 the quick brown fox jumps over the lazy dog . dog2 the quick brown fox jumps over the lazy dog . dog3 the quick brown fox jumps over the lazy cat . dog4 the quick brown fox jumps over the lazy dog . dog5 the quick brown fox jumps over the lazy dog . dog可以看出,将修改的第三行内容存在了text文件中[root@zutuanxue ~]# cat text 3 the quick brown fox jumps over the lazy cat . dog

三、练习案例

3.1、写一个初始化系统的脚本 案例需求 1)自动修改主机名(如:ip是192.168.0.88,则主机名改为server88.zutuanxue.cc)

a. 更改文件非交互式 sed

/etc/sysconfig/network

b.将本主机的IP截取出来赋值给一个变量ip;再然后将ip变量里以.分割的最后一位赋值给另一个变量ip1

2)自动配置可用的yum源

3)自动关闭防火墙和selinux

代码语言:javascript代码运行次数:0运行复制

1、关闭防火墙2、关闭selinux3、配置yum源4、ntp时间服务器 5、更新系统软件包

3.2、写一个搭建ftp服务的脚本,要求如下: 案例需求 1)不支持本地用户登录local_enable=NO 2) 匿名用户可以上传 新建 删除 anon_upload_enable=YES anon_mkdir_write_enable=YES 3) 匿名用户限速500KBps anon_max_rate=500000

代码语言:javascript代码运行次数:0运行复制

仅供参考:#!/bin/bashipaddr=`ifconfig eth0|sed -n '2p'|sed -e 's/.*inet addr:(.*) Bcast.*/1/g'`iptail=`echo $ipaddr|cut -d'.' -f4`ipremote=192.168.1.10#修改主机名hostname server$iptail.zutuanxue.comsed -i "/HOSTNAME/cHOSTNAME=server$iptail.zutuanxue.com" /etc/sysconfig/networkecho "$ipaddr server$iptail.zutuanxue.cc" >>/etc/hosts#关闭防火墙和selinuxservice iptables stopsetenforce 0 >/dev/null 2>&1sed -i '/^SELINUX=/cSELINUX=disabled' /etc/selinux/config#配置yum源(一般是内网源)#test networkping -c 1 $ipremote > /dev/null 2>&1if [ $? -ne 0 ];thenecho "你的网络不通,请先检查你的网络"exit 1elseecho "网络ok."ficat > /etc/yum.repos.d/server.repo </dev/null#备份配置文件conf=/etc/vsftpd/vsftpd.confcp $conf $conf.default#根据需求修改配置文件sed -ir '/^#|^$/d' $confsed -i '/local_enable/clocal_enable=NO' $confsed -i '$a anon_upload_enable=YES' $confsed -i '$a anon_mkdir_write_enable=YES' $confsed -i '$a anon_other_write_enable=YES' $confsed -i '$a anon_max_rate=512000' $conf#启动服务service vsftpd restart &>/dev/null && echo"vsftpd服务启动成功"#测试验证chmod 777 /var/ftp/pubcp /etc/hosts /var/ftp/pub#测试下载cd /tmplftp $ipaddr <<endcd pubget hostsexitendif [ -f /tmp/hosts ];thenecho "匿名用户下载成功"rm -f /tmp/hostselseecho "匿名用户下载失败"fi#测试上传、创建目录、删除目录等cd /tmplftp $ipaddr << endcd pubmkdir test1mkdir test2put /etc/grouprmdir test2exitendif [ -d /var/ftp/pub/test1 ];then    echo "创建目录成功"if [ ! -d /var/ftp/pub/test2 ];then    echo "文件删除成功"        fielseif [ -f /var/ftp/pub/group ];thenecho "文件上传成功"        else        echo "上传、创建目录删除目录部ok"        fi fi   [ -f /var/ftp/pub/group ] && echo "上传文件成功"

以上就是shell对文件的操作的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/459767.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年11月8日 03:00:42
下一篇 2025年11月8日 03:04:22

相关推荐

  • Uniapp 中如何不拉伸不裁剪地展示图片?

    灵活展示图片:如何不拉伸不裁剪 在界面设计中,常常需要以原尺寸展示用户上传的图片。本文将介绍一种在 uniapp 框架中实现该功能的简单方法。 对于不同尺寸的图片,可以采用以下处理方式: 极端宽高比:撑满屏幕宽度或高度,再等比缩放居中。非极端宽高比:居中显示,若能撑满则撑满。 然而,如果需要不拉伸不…

    2025年12月24日
    400
  • 如何让小说网站控制台显示乱码,同时网页内容正常显示?

    如何在不影响用户界面的情况下实现控制台乱码? 当在小说网站上下载小说时,大家可能会遇到一个问题:网站上的文本在网页内正常显示,但是在控制台中却是乱码。如何实现此类操作,从而在不影响用户界面(UI)的情况下保持控制台乱码呢? 答案在于使用自定义字体。网站可以通过在服务器端配置自定义字体,并通过在客户端…

    2025年12月24日
    800
  • 如何在地图上轻松创建气泡信息框?

    地图上气泡信息框的巧妙生成 地图上气泡信息框是一种常用的交互功能,它简便易用,能够为用户提供额外信息。本文将探讨如何借助地图库的功能轻松创建这一功能。 利用地图库的原生功能 大多数地图库,如高德地图,都提供了现成的信息窗体和右键菜单功能。这些功能可以通过以下途径实现: 高德地图 JS API 参考文…

    2025年12月24日
    400
  • 如何使用 scroll-behavior 属性实现元素scrollLeft变化时的平滑动画?

    如何实现元素scrollleft变化时的平滑动画效果? 在许多网页应用中,滚动容器的水平滚动条(scrollleft)需要频繁使用。为了让滚动动作更加自然,你希望给scrollleft的变化添加动画效果。 解决方案:scroll-behavior 属性 要实现scrollleft变化时的平滑动画效果…

    2025年12月24日
    000
  • 如何为滚动元素添加平滑过渡,使滚动条滑动时更自然流畅?

    给滚动元素平滑过渡 如何在滚动条属性(scrollleft)发生改变时为元素添加平滑的过渡效果? 解决方案:scroll-behavior 属性 为滚动容器设置 scroll-behavior 属性可以实现平滑滚动。 html 代码: click the button to slide right!…

    2025年12月24日
    500
  • 如何选择元素个数不固定的指定类名子元素?

    灵活选择元素个数不固定的指定类名子元素 在网页布局中,有时需要选择特定类名的子元素,但这些元素的数量并不固定。例如,下面这段 html 代码中,activebar 和 item 元素的数量均不固定: *n *n 如果需要选择第一个 item元素,可以使用 css 选择器 :nth-child()。该…

    2025年12月24日
    200
  • 使用 SVG 如何实现自定义宽度、间距和半径的虚线边框?

    使用 svg 实现自定义虚线边框 如何实现一个具有自定义宽度、间距和半径的虚线边框是一个常见的前端开发问题。传统的解决方案通常涉及使用 border-image 引入切片图片,但是这种方法存在引入外部资源、性能低下的缺点。 为了避免上述问题,可以使用 svg(可缩放矢量图形)来创建纯代码实现。一种方…

    2025年12月24日
    100
  • 如何解决本地图片在使用 mask JS 库时出现的跨域错误?

    如何跨越localhost使用本地图片? 问题: 在本地使用mask js库时,引入本地图片会报跨域错误。 解决方案: 要解决此问题,需要使用本地服务器启动文件,以http或https协议访问图片,而不是使用file://协议。例如: python -m http.server 8000 然后,可以…

    2025年12月24日
    200
  • 如何让“元素跟随文本高度,而不是撑高父容器?

    如何让 元素跟随文本高度,而不是撑高父容器 在页面布局中,经常遇到父容器高度被子元素撑开的问题。在图例所示的案例中,父容器被较高的图片撑开,而文本的高度没有被考虑。本问答将提供纯css解决方案,让图片跟随文本高度,确保父容器的高度不会被图片影响。 解决方法 为了解决这个问题,需要将图片从文档流中脱离…

    2025年12月24日
    000
  • 为什么 CSS mask 属性未请求指定图片?

    解决 css mask 属性未请求图片的问题 在使用 css mask 属性时,指定了图片地址,但网络面板显示未请求获取该图片,这可能是由于浏览器兼容性问题造成的。 问题 如下代码所示: 立即学习“前端免费学习笔记(深入)”; icon [data-icon=”cloud”] { –icon-cl…

    2025年12月24日
    200
  • 如何利用 CSS 选中激活标签并影响相邻元素的样式?

    如何利用 css 选中激活标签并影响相邻元素? 为了实现激活标签影响相邻元素的样式需求,可以通过 :has 选择器来实现。以下是如何具体操作: 对于激活标签相邻后的元素,可以在 css 中使用以下代码进行设置: li:has(+li.active) { border-radius: 0 0 10px…

    2025年12月24日
    100
  • 如何模拟Windows 10 设置界面中的鼠标悬浮放大效果?

    win10设置界面的鼠标移动显示周边的样式(探照灯效果)的实现方式 在windows设置界面的鼠标悬浮效果中,光标周围会显示一个放大区域。在前端开发中,可以通过多种方式实现类似的效果。 使用css 使用css的transform和box-shadow属性。通过将transform: scale(1.…

    2025年12月24日
    200
  • 为什么我的 Safari 自定义样式表在百度页面上失效了?

    为什么在 Safari 中自定义样式表未能正常工作? 在 Safari 的偏好设置中设置自定义样式表后,您对其进行测试却发现效果不同。在您自己的网页中,样式有效,而在百度页面中却失效。 造成这种情况的原因是,第一个访问的项目使用了文件协议,可以访问本地目录中的图片文件。而第二个访问的百度使用了 ht…

    2025年12月24日
    000
  • 如何用前端实现 Windows 10 设置界面的鼠标移动探照灯效果?

    如何在前端实现 Windows 10 设置界面中的鼠标移动探照灯效果 想要在前端开发中实现 Windows 10 设置界面中类似的鼠标移动探照灯效果,可以通过以下途径: CSS 解决方案 DEMO 1: Windows 10 网格悬停效果:https://codepen.io/tr4553r7/pe…

    2025年12月24日
    000
  • 使用CSS mask属性指定图片URL时,为什么浏览器无法加载图片?

    css mask属性未能加载图片的解决方法 使用css mask属性指定图片url时,如示例中所示: mask: url(“https://api.iconify.design/mdi:apple-icloud.svg”) center / contain no-repeat; 但是,在网络面板中却…

    2025年12月24日
    000
  • 如何用CSS Paint API为网页元素添加时尚的斑马线边框?

    为元素添加时尚的斑马线边框 在网页设计中,有时我们需要添加时尚的边框来提升元素的视觉效果。其中,斑马线边框是一种既醒目又别致的设计元素。 实现斜向斑马线边框 要实现斜向斑马线间隔圆环,我们可以使用css paint api。该api提供了强大的功能,可以让我们在元素上绘制复杂的图形。 立即学习“前端…

    2025年12月24日
    000
  • 图片如何不撑高父容器?

    如何让图片不撑高父容器? 当父容器包含不同高度的子元素时,父容器的高度通常会被最高元素撑开。如果你希望父容器的高度由文本内容撑开,避免图片对其产生影响,可以通过以下 css 解决方法: 绝对定位元素: .child-image { position: absolute; top: 0; left: …

    2025年12月24日
    000
  • 为什么自定义样式表在 Safari 中访问百度页面时无法生效?

    自定义样式表在 safari 中失效的原因 用户尝试在 safari 偏好设置中添加自定义样式表,代码如下: body { background-image: url(“/users/luxury/desktop/wallhaven-o5762l.png”) !important;} 测试后发现,在…

    2025年12月24日
    000
  • 使用 Mask 导入本地图片时,如何解决跨域问题?

    跨域疑难:如何解决 mask 引入本地图片产生的跨域问题? 在使用 mask 导入本地图片时,你可能会遇到令人沮丧的跨域错误。为什么会出现跨域问题呢?让我们深入了解一下: mask 框架假设你以 http(s) 协议加载你的 html 文件,而当使用 file:// 协议打开本地文件时,就会产生跨域…

    2025年12月24日
    200
  • CSS 帮助

    我正在尝试将文本附加到棕色框的左侧。我不能。我不知道代码有什么问题。请帮助我。 css .hero { position: relative; bottom: 80px; display: flex; justify-content: left; align-items: start; color:…

    2025年12月24日 好文分享
    200

发表回复

登录后才能评论
关注微信