前端学习——点击图片在屏幕中间弹出大图

浏览网页的的时候经常会遇到网页中放置许多略缩图,点击图片时则会弹出大图,那么这又是怎么实现的呢?这里做了一个小demo,记录一下自己的思路

这个是原生javascript,具体的css样式可以自行修改

 
<meta charset="utf-8">
<html>
<head>
    <title>title>
    <style type="text/css">
	#smallImg{
      
		width: 50%;
	}
    .black_overlay{
      
        display: none;
        position: absolute;
        top: 0%;left: 0%;
        width: 100%;
        height: 100%;
        background-color: black;
        z-index: 1001;
        opacity: .70;
    }
    .white_content{
      
        display: none;
        position: fixed;
        top: 20%;
		left: 20%;
		width: 60%;
		height: 60%;
		background-color: white;
		z-index: 1002;
		overflow: auto;

	}
	#bigImg{
      
		width: 100%;
		height: 100%;
	}
style>
head>
<body>
<img src="D:/codehsc/changchun/changchunyinji/ccyj2/public/img/fzlc/柳条边示意图.gif"	onclick="document.getElementById('light').style.display='block';
    document.getElementById('fade').style.display='block'" id="smallImg"/>

<div id="light" class="white_content">
<img src="D:/codehsc/changchun/changchunyinji/ccyj2/public/img/fzlc/柳条边示意图.gif"
    onclick="document.getElementById('light').style.display='none';
    document.getElementById('fade').style.display='none'" id="bigImg"/>
div>
<div id="fade" class="black_overlay">div>
body>
html>

这个是在公司用vue做的项目的代码片段,有了v-show的帮助,实现起来还是简单了许多的

<div class="photoList">
      <div class="photoList-wrapper" :style="`transform: translateX(${
        translateX}px);`">
        <div
          class="photoItem"
          v-for="(item,index) in newFZLC[years[index].name].slice(sliceIndex,sliceIndex+2)"
          :key="index"
          :style="`transform: rotate(${
        ramdomArray[index]}deg)`"
          @click="clickItem(item)"
        >
          <img :src="`图片路径`" @error="onerror($event)" @click="bigImg(item.code)" />
          <p>{
    {item.code}}p>
        div>
      div>
    div>

    <TimeBar
      :array="years.map(e => e.name)"
      :toolName="'返回'"
      @change="changeIndex"
      @divSrc="showPhotoCard"
    />
    <div id="light" class="white_content" v-show="bigImgShow" @click="bigImg">
      <img :src="图片路径" />
    div>
    <div id="fade" class="black_overlay"  v-show="bigImgShow">div>
  div>
<script>
   export default {
      
        methods: {
      
          bigImg(isrc) {
      
          this.bigImgShow = !this.bigImgShow;
          this.imgSrc=isrc;
          debugger
          },
      	}
   }
script>
<style>
.white_content {
      
  position: fixed;
  top: 20%;
  left: 20%;
  width: 60%;
  height: 60%;
  background-color: white;
  z-index: 1005;
  overflow: auto;
  img{
      
    width: 100%;
    height: 100%;
  }
}
style>

你可能感兴趣的:(前端,html,javascript,vue,js,css)