把当前img作为到爷爷的背景图

news/2024/9/1 2:36:02 标签: javascript, 前端

(忽略图大小不一致,一般UI给的图会刚好适合页面大小,我这网上找的图,难调大小,我行内的就自己随便写的宽高),另外悄悄告诉你最后有简单方法~~

先来看看初始DOM结构代码


<template>
  <div id="app">
    <div class="bodyy">
      <div class="grandparent">
        <div class="parent">
          <img class="imgClass" style="width: 320px; height:120px" src="https://img0.baidu.com/it/u=2190700831,1994064510&fm=253&fmt=auto&app=138&f=JPEG?w=667&h=500">
        </div>
        <div class="brother1">222</div>
        <div class="brother2">333</div>
        <div>5555</div>
      </div>
    </div>

  </div>
</template>

最开始图片在他本来的DOM结构里面,爷爷也没有背景,这样的DOM是把两个叔叔抵下去的
在这里插入图片描述

在这里插入图片描述

假设我们已经固定了图片为孙子,但是我们想要这个图片作为整个爷爷的背景图,想要其他该在爷爷上边的元素的层级不变,也就是这样的DOM结构:
在这里插入图片描述
效果是这样
在这里插入图片描述

话不多说,直接上代码:(这里要通过img的class获取爷爷元素,所以用v-show当满足某种条件才渲染这种我需要的样式,倘若知道爷爷元素和图片的大小直接v-if


<template>
  <div id="app">
    <div class="bodyy">
      <div class="grandparent">
        <div class="parent">
          <img class="imgClass" v-show="a != 1" src="https://img2.baidu.com/it/u=443161650,959310912&fm=253&fmt=auto&app=138&f=JPEG?w=888&h=500">
        </div>
        <div class="brother1">222</div>
        <div class="brother2">333</div>
      </div>
    </div>

  </div>
</template>

<script>

import SlotDemo from '@/views/slotDemo/index.vue'
export default {
  name: 'App',
  data() {
    return {
      a: 1
    }
  },
  components: {
    SlotDemo
  },
  mounted() {
    if(this.a == 1) {
      console.log('000000')
      this.showImg()
    }
  },
  methods: {
    showImg() {
    // 获取到当前 <img> 元素
    // var imgElement = document.querySelector('img');
    var imgElement = document.getElementsByClassName('imgClass')
    console.log('999', imgElement.width, imgElement.height, typeof(imgElement.height + 'px'))
    let numm = imgElement.height + 'px'
    console.log('numm', numm)
    // 获取当前 <img> 元素的父元素的父元素
    var grandParentElement = imgElement[0].parentElement.parentElement;
    console.log('grandParentElement', grandParentElement)
    // 设置父元素的父元素的背景图
    grandParentElement.style.backgroundImage = `url('https://img0.baidu.com/it/u=2190700831,1994064510&fm=253&fmt=auto&app=138&f=JPEG?w=667&h=500')`;
    grandParentElement.style.backgroundSize = 'cover';  // 设置背景图片的尺寸适应父元素大小
    grandParentElement.style.height = numm; 

    },
    
  }
  
}
</script>

<style >
*{
  margin: 0;
  padding: 0;
}
.bodyy{
  background: #798866;
  z-index: 8888;
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  .grandparent{
    width: 100%;
    height: auto;
    position: absolute;
    bottom: 0;
    min-height: 3.5rem;
    background: #ffffff;
    border-top-left-radius: 0.48rem;
    border-top-right-radius: 0.48rem;
    .parent{
      background: blue;
      img{
      
      }
    }
    .brother1{
      //background: white; 
      position: relative;
      height: auto;
      margin-top: 15rem;

    }
    .brother2{
     
      display: flex;
      flex-direction: column;
      align-content: center;
      justify-content: center;
      margin-top: 0.36rem;
      margin-bottom: 0.68rem;
      width: 100%;
      height: auto;
      position: relative;
    }
}

}

</style>

TIPS:有个简单单方案就是直接给parent加一个margin-bottom!!
在这里插入图片描述


http://www.niftyadmin.cn/n/5561671.html

相关文章

C++--copy

copy 拷贝,把源迭代器区间的值拷贝到目的迭代器。使用者保证目的空间足够。时间复杂度O(n)。 函数声明如下: template<class InputIterator, class OutputIterator> OutputIterator copy( InputIterator _First, //源开始迭代器 InputIterator _Last, //源结束迭代…

四六级词汇小程序的设计

管理员账户功能包括&#xff1a;系统首页&#xff0c;个人中心&#xff0c;用户管理&#xff0c;英语词汇管理&#xff0c;易错词管理&#xff0c;学习笔记管理&#xff0c;签到打卡管理&#xff0c;论坛管理 微信端账号功能包括&#xff1a;系统首页&#xff0c;英语词汇&…

C++写一个线程池

C写一个线程池 文章目录 C写一个线程池设计思路测试数据的实现任务类的实现线程池类的实现线程池构造函数线程池入口函数队列中取任务添加任务函数线程池终止函数 源码 之前用C语言写了一个线程池&#xff0c;详情请见&#xff1a; C语言写一个线程池 这次换成C了&#xff01;…

beego框架_golang web框架_使用介绍

beego简介 beego是一个用于快速开发Go应用的http框架&#xff0c;由Go语言方面的技术大牛设计。beego可以用来快速开发API、Web、后端服务等各种应用&#xff0c;是一个RESTful的框架&#xff0c;主要设计灵感来源于tornado、sinatra、flask这三个框架&#xff0c;但结合了Go本…

SwiftUI 6.0(Xcode 16)新 PreviewModifier 协议让预览调试如虎添翼

概览 用 SwiftUI 框架开发过应用的小伙伴们都知道&#xff0c;SwiftUI 中的视图由各种属性和绑定“扑朔迷离”的缠绕在一起&#xff0c;自成体系。 想要在 Xcode 预览中泰然处之的调试 SwiftUI 视图有时并不是件容易的事。其中&#xff0c;最让人秃头码农们头疼的恐怕就要数如…

防火墙实验(三)双机热备与带宽管理

要求12&#xff1a; 防火墙1&#xff1a; 防火墙2&#xff1a; 以上完成负载分担&#xff0c;接下来在防火墙1配置策略然后同步到防火墙3上 要求13&#xff1a; 要求14&#xff1a; 条件15在防火墙3上配置&#xff0c;配置之前需要将双机热备关闭 要求16&#xff1a; 至此实验…

【Qt】QWidget在隐藏时,发送信号

1. 介绍 Qt中&#xff0c;QWidget隐藏时&#xff0c;默认不发送信号。需要重写hideEvent()函数实现。 2. 代码 #include <QWidget> class MyWidget : public QWidget {Q_OBJECT signals:void hiddenStateChanged(bool isHidden);protected:void hideEvent(QHideEvent*…

深入理解 Git Reset 的三种模式及其使用场景

&#x1f34e;个人博客&#xff1a;个人主页 &#x1f3c6;个人专栏&#xff1a;Android ⛳️ 功不唐捐&#xff0c;玉汝于成 目录 前言 正文 1. --soft 模式 2. --mixed 模式&#xff08;默认&#xff09; 3. --hard 模式 总结 结语 我的其他博客 前言 在日常的开发…