全屏展开转换并将元素样式设置为子

时间:2018-02-10 12:16:52

标签: javascript html css css3

英语不是我的第一语言所以很难解释细节,但我会尽我所能努力。我真的很抱歉。

我正在缩略图扩展全屏转换。 与google photos类似,缩略图应展开至全屏转换动画也应适用。

我的方法是点击元素的克隆,然后设置初始样式(顶部和左侧,宽度和高度等)与原始元素
添加类将位置设置为零,并进行完全展开。宽度:100vw和高度:100vh,顶部:0左:0,位置:固定(类.fullscreen)是它。 我在http://jsfiddle.net/a7nzy6w6/299/借了一些想法。

但在设置样式时,

clone.style.top = rect.top;
clone.style.left = rect.left;
clone.style.height = img.offsetHeight
clone.style.width = img.offsetWidth

此方法将替换所有子类的顶部,左侧和高度宽度。即使它也会忽略“全屏”类

所以它不会变换或扩展并保持原始风格。如果我没有设置样式,则转换动画将不适用。


我如何 应用全屏展开变换动画?有没有更好的解决方案? 或我如何 将元素的初始样式设置为子样式而不替换javascript中添加的类


再一次,我真的很抱歉我的英语。我尽力了  顺便说一下,我不知道为什么element.style不能用于片段

function handler(element)
{
	var type = element.getAttribute("data-type")
  switch(type)
	{
    case "image":
      transition_fullscreen(element);
      break;
    default:
      break;
  }
}
function transition_fullscreen(element)
{
  var img = element.getElementsByClassName('el_view')[0];
	var rect = img.getBoundingClientRect();
	var clone = img.cloneNode(true);
  clone.style.top = rect.top;
	clone.style.left = rect.left;
  clone.style.height = img.offsetHeight
  clone.style.width = img.offsetWidth
	clone.classList.add('fullscreen');
  var ap = document.getElementById('form').appendChild(clone);
	
}
#form 
{
	width: 100%;
	text-align:center;
}

#form .element 
{
	display:inline-block;
	float:left;
	width: 10%;
	height: 20%;
	margin: 1.9em;
	cursor: default; 
	transition: background .1s ease-in-out;
	animation:animatezoom 0.5s;
}

#form .highlight
{
	
	padding:14px;
	transition: transform .1s ease-out;
	padding-top:auto;
	/*border: 1px solid #ddd;
    border-radius: 4px;*/
}
#form .highlight:hover { transform: translateY(-0.5rem) scale(1.0125);
	box-shadow: 0 0.5em 1.9rem -1rem rgba(0,0,0,0.5); }
#form .highlight:active { transform:scale(0.8); }

#form .el_img { max-height: 124px; vertical-align: middle; }
#form .el_img img { max-width: 88px; max-height: 124px; margin-top: 5%; border-radius:5px; box-shadow:0 2px 5px 0 rgba(0,0,0,0.16),0 2px 10px 0 rgba(0,0,0,0.12); border-radius:5px;
opacity: 1;
transition: all 3s;
}

#form .el_img .fullscreen
{
	z-index:9999;
	max-width:100vw;
	max-height:100vh;
	width:100vw;
	height:100vh;
	position:fixed;
	top:1%;
	left:1%;
  transition: all 3s;
}
<div id="form">
  <div id="element#somefile.exe" class="element" data-type="image" data-link="somefile.exe" onclick=handler(this); title="somefile.exe"> 
          <div id="highlight#somefile.exe" class="highlight"> 
            <div id="content#somefile.exe" class="content"> 
              <div id="el_img#somefile.exe" class="el_img"> 
                <img id="view#somefile.exe" class="el_view" src="https://lh3.googleusercontent.com/oKsgcsHtHu_nIkpNd-mNCAyzUD8xo68laRPOfvFuO0hqv6nDXVNNjEMmoiv9tIDgTj8=w170"> 
              </div> 
              <div id="el_name#somefile.exe" class="el_name"> 
                somefile.exe 
              </div> 
            </div> 
          </div> 
        </div>
        <div id="element#somefile.exe" class="element" data-type="image" data-link="somefile.exe" onclick=handler(this); title="somefile.exe"> 
          <div id="highlight#somefile.exe" class="highlight"> 
            <div id="content#somefile.exe" class="content"> 
              <div id="el_img#somefile.exe" class="el_img"> 
                <img id="view#somefile.exe" class="el_view" src="https://lh3.googleusercontent.com/hYQO8ekNvsPWMSzMe6IZdCAT6p8qq-SlzA0jiZstV7qBcWg5kn-39qHY0ZaBPqd3usc=w170"> 
              </div> 
              <div id="el_name#somefile.exe" class="el_name"> 
                blahblah.exe
              </div> 
            </div> 
          </div> 
        </div>
        <div id="element#somefile.exe" class="element" data-type="image" data-link="somefile.exe" onclick=handler(this); title="somefile.exe"> 
          <div id="highlight#somefile.exe" class="highlight"> 
            <div id="content#somefile.exe" class="content"> 
              <div id="el_img#somefile.exe" class="el_img"> 
                <img id="view#somefile.exe" class="el_view" src="https://lh3.googleusercontent.com/UMB2HRRRAAzXAEaCM9Gg-baCaDx_1RTXHscW5k2Ge3P4KP4mwTt2m6oyEHBWex3c4SxU=w300"> 
              </div> 
              <div id="el_name#somefile.exe" class="el_name"> 
                mehhhhh.cool
              </div> 
            </div> 
          </div> 
        </div>
    </div>
    


2 个答案:

答案 0 :(得分:0)

我只是把它像Google照片一样。 通过添加一些HTML,CSS和JS。 这适用于您的所有产品 演示:

let modal = document.getElementById('myModal'),
  modalImg = document.getElementById('img01'),
  captionText = document.getElementById('caption');

function handler(e) {
  switch (e.getAttribute('data-type')) {
    case 'image':
      transition_fullscreen(e);
  }
}

function transition_fullscreen(e) {
  modal.style.display = 'block',
    modalImg.src = e.children[0].children[0].children[0].children[0].src, captionText.innerHTML = e.children[0].children[0].children[1].innerHTML;
}
const close_btn = document.getElementsByClassName('close')[0];
close_btn.onclick = function() {
    modal.style.display = 'none';
  },
  window.onclick = function(e) {
    e.target == modal && (modal.style.display = 'none');
  };
#myImg,
.close {
  transition: .3s;
}

body {
  font-family: Arial, Helvetica, sans-serif;
}

#myImg {
  border-radius: 5px;
  cursor: pointer;
}

#myImg:hover {
  opacity: .7;
}

.modal {
  display: none;
  position: fixed;
  z-index: 1;
  padding-top: 100px;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  overflow: auto;
  background-color: #000;
  background-color: rgba(0, 0, 0, .9);
}

#caption,
.modal-content {
  margin: auto;
  display: block;
  width: 80%;
  max-width: 700px;
}

#caption {
  text-align: center;
  color: #ccc;
  padding: 10px 0;
  height: 150px;
}

#caption,
.modal-content {
  -webkit-animation-name: zoom;
  -webkit-animation-duration: .6s;
  animation-name: zoom;
  animation-duration: .6s;
}

@-webkit-keyframes zoom {
  from {
    -webkit-transform: scale(0);
  }
  to {
    -webkit-transform: scale(1);
  }
}

@keyframes zoom {
  from {
    transform: scale(0);
  }
  to {
    transform: scale(1);
  }
}

.close {
  position: absolute;
  top: 15px;
  right: 35px;
  color: #f1f1f1;
  font-size: 40px;
  font-weight: 700;
}

.close:focus,
.close:hover {
  color: #bbb;
  text-decoration: none;
  cursor: pointer;
}

@media only screen and (max-width:700px) {
  .modal-content {
    width: 100%;
  }
}
<!DOCTYPE html>
<html>

<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">

</head>

<body>
  <div id="form">
    <div id="element#somefile.exe" class="element" data-type="image" data-link="somefile.exe" onclick=handler(this); title="somefile.exe">
      <div id="highlight#somefile.exe" class="highlight">
        <div id="content#somefile.exe" class="content">
          <div id="el_img#somefile.exe" class="el_img">
            <img id="view#somefile.exe" class="el_view" src="https://lh3.googleusercontent.com/oKsgcsHtHu_nIkpNd-mNCAyzUD8xo68laRPOfvFuO0hqv6nDXVNNjEMmoiv9tIDgTj8=w170">
          </div>
          <div id="el_name#somefile.exe" class="el_name">
            somefile.exe
          </div>
        </div>
      </div>
    </div>
    <div id="element#somefile.exe" class="element" data-type="image" data-link="somefile.exe" onclick=handler(this); title="somefile.exe">
      <div id="highlight#somefile.exe" class="highlight">
        <div id="content#somefile.exe" class="content">
          <div id="el_img#somefile.exe" class="el_img">
            <img id="view#somefile.exe" class="el_view" src="https://lh3.googleusercontent.com/hYQO8ekNvsPWMSzMe6IZdCAT6p8qq-SlzA0jiZstV7qBcWg5kn-39qHY0ZaBPqd3usc=w170">
          </div>
          <div id="el_name#somefile.exe" class="el_name">
            blahblah.exe
          </div>
        </div>
      </div>
    </div>
    <div id="element#somefile.exe" class="element" data-type="image" data-link="somefile.exe" onclick=handler(this); title="somefile.exe">
      <div id="highlight#somefile.exe" class="highlight">
        <div id="content#somefile.exe" class="content">
          <div id="el_img#somefile.exe" class="el_img">
            <img id="view#somefile.exe" class="el_view" src="https://lh3.googleusercontent.com/UMB2HRRRAAzXAEaCM9Gg-baCaDx_1RTXHscW5k2Ge3P4KP4mwTt2m6oyEHBWex3c4SxU=w300">
          </div>
          <div id="el_name#somefile.exe" class="el_name">
            mehhhhh.cool
          </div>
        </div>
      </div>
    </div>
  </div>
  <!-- The Modal -->
  <div id="myModal" class="modal">
    <span class="close">&times;</span>
    <img class="modal-content" id="img01">
    <div id="caption"></div>
  </div>



</body>

</html>

答案 1 :(得分:0)

我创立了它:
在CSS中,您搜索的图像如下:
#form .el_img .fullscreen
这会搜索位于#form的{​​{1}}中的元素,并且它们具有.el_img类 但不是fullsceen中同时包含#formel_img类的元素。 因此,您需要从选择中移除fullscreen之前的空格,使其如下所示:

.el_img

它会起作用。
演示:https://www.w3schools.com/code/tryit.asp?filename=FOBUOKP41CYW