背景图片实现多图片拼接,并不出现缝隙或重叠

一些页面中需要有指定的背景图,宽度并不相同,需要适应各种尺寸的容器。因此需要把单个图片拆分成多个,部分图片进行拉伸。以适应容器宽度。css提供了该方法,配合使用background-image,background-position(横轴图片起始位置),background-size(第一组:第一张图片横轴结束位置;第二组:需要拉伸图片的区域宽度),background-repeat

效果如图:

标题背景用两张背景图片生成,可适应多种尺寸的区域使用。

div.title-tag {
    line-height: 26px;
    height: 54px;
    text-align: left;
    padding: 14px 15px 14px 52px;
    box-sizing: border-box;// 如果div有padding或者margin使用该属性后,宽高不需要考虑去除padding或者margin的像素值
    background-image: url('../../assets/image/title-bg-l.png'), url('../../assets/image/title-bg-r.png');
    background-position: 0%, 400px;
    background-size: 400px 100%, calc(100% - 400px) 100%;
    background-repeat: no-repeat, no-repeat;
    // 使用svg当背景
    // background: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' class='design-iconfont'><defs><linearGradient id='cxwv9jw2o__mdn7kntoma' x1='.032' y1='1' x2='1' y2='1' gradientUnits='objectBoundingBox'><stop offset='0' stop-color='%2319a2ff' stop-opacity='.2'/><stop offset='1' stop-color='%2319a2ff' stop-opacity='0'/></linearGradient><linearGradient id='cxwv9jw2o__vn7afj05sb' x1='.074' y1='1' x2='.996' y2='1' gradientUnits='objectBoundingBox'><stop offset='0' stop-color='%2319a2ff' stop-opacity='.749'/><stop offset='.356' stop-color='%2319a2ff' stop-opacity='.302'/><stop offset='.818' stop-color='%2319a2ff' stop-opacity='.102'/><stop offset='1' stop-color='%2319a2ff' stop-opacity='0'/></linearGradient><linearGradient id='cxwv9jw2o__084os828uc' x1='.161' x2='.721' y2='.806' gradientUnits='objectBoundingBox'><stop offset='0' stop-color='%23ffda23'/><stop offset='1' stop-color='%23ff9d23'/></linearGradient><linearGradient id='cxwv9jw2o__65dnsr944e' y1='1' x2='1' y2='1' gradientUnits='objectBoundingBox'><stop offset='0' stop-color='%2319a2ff' stop-opacity='0'/><stop offset='.307' stop-color='%2319f7ff'/><stop offset='.507' stop-color='%23fff'/><stop offset='.693' stop-color='%2319e7ff'/><stop offset='1' stop-color='%2319a2ff' stop-opacity='0'/></linearGradient><radialGradient id='cxwv9jw2o__5pnmrq3ypd' cx='.5' cy='1' r='.5' gradientTransform='matrix(1 0 0 2 0 -1)' gradientUnits='objectBoundingBox'><stop offset='0' stop-color='%2316dfff' stop-opacity='.49'/><stop offset='1' stop-color='%23b4f2ff' stop-opacity='0'/></radialGradient></defs><path d='M249.355,175c-7.805-.037-13.392-11.173-13.392-11.173L213.491,125H975v50.016H863.385C514.429,175.017,251.957,175.013,249.355,175Z' transform='translate(-208.001 -123)' fill='url(%23cxwv9jw2o__mdn7kntoma)'/><path d='M249.482,178a12.016,12.016,0,0,1-6.48-2.081,21.855,21.855,0,0,1-4.692-4.221,33.52,33.52,0,0,1-4.018-5.911L210.016,124H975v2H213.491l22.557,38.827s5.609,11.137,13.443,11.173c2.613.012,266.086.016,616.371.016H975v2H865.862C459.176,178.016,251.8,178.011,249.482,178Z' transform='translate(-208.001 -124)' fill='url(%23cxwv9jw2o__vn7afj05sb)'/><path d='M232.139,148h-5.773l-13.856-24h5.773l13.855,24Z' transform='translate(-212.51 -106)' fill='url(%23cxwv9jw2o__084os828uc)'/><path transform='translate(39.49 27)' fill='url(%23cxwv9jw2o__5pnmrq3ypd)' d='M0 0H197V25H0z'/><path transform='translate(41.49 52)' fill='url(%23cxwv9jw2o__65dnsr944e)' d='M0 0H180V2H0z'/></svg>") center center;
}

3张背景图示例:

.background {
  cursor: pointer;
  height: 90px;
  min-width: 350px;// 与width: fit-content;共同使用,实现自身的宽度自适应,而不使其余拥有相同class的元素影响宽度
  width: fit-content;
  display: flex;
  align-items: center;
  justify-content: flex-start;
  position: relative;
  background-image: url('../../assets/image/map-svg-background-l.png'), url('../../assets/image/map-svg-background-m.png'),
    url('../../assets/image/map-svg-background-r.png');// 第一张图165px,第三张图105px,现在需要使第二张图进行拉伸,则使用background-position和background-size进行计算
  background-position: left, 165px, right;
  background-size: 165px 100%, calc(100% - 165px - 105px) 100%, 105px 100%;
  background-repeat: no-repeat, no-repeat, no-repeat;

}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Java的ImageIO和BufferedImage类实现多张图片拼接。下面是一个示例代码,将多张图片纵向拼接一张图片: ```java import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; public class ImageConcatenation { public static void main(String[] args) throws IOException { // 设置图片路径 String[] imagePaths = new String[]{"image1.jpg", "image2.jpg", "image3.jpg"}; // 读取第一张图片 BufferedImage resultImage = ImageIO.read(new File(imagePaths[0])); // 获取图片宽度和高度 int width = resultImage.getWidth(); int height = resultImage.getHeight(); // 循环读取剩余的图片,并拼接到结果图片中 for (int i = 1; i < imagePaths.length; i++) { BufferedImage nextImage = ImageIO.read(new File(imagePaths[i])); height += nextImage.getHeight(); // 创建新的空白图片 BufferedImage combined = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // 将原始图片拷贝到新的图片中 combined.getGraphics().drawImage(resultImage, 0, 0, null); // 将下一张图片拷贝到新的图片中 combined.getGraphics().drawImage(nextImage, 0, resultImage.getHeight(), null); // 更新结果图片 resultImage = combined; } // 保存拼接后的图片 ImageIO.write(resultImage, "jpg", new File("result.jpg")); } } ``` 如果要将多张图片横向拼接,只需要将循环中的高度改为宽度,将resultImage和nextImage的高度改为宽度,以及将drawImage方法的参数修改为(resultImage.getWidth(), 0)和(nextImage.getWidth(), 0)即可。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值