【问题标题】:Fullscreen expand transform and Set element style as a child全屏展开变换并将元素样式设置为子项
【发布时间】:2018-07-21 02:24:00
【问题描述】:

英语不是我的母语,所以很难解释细节,但我会尽我所能。我真的很抱歉。

我正在制作缩略图扩展全屏过渡。 像 google photos 一样,缩略图应该扩展到全屏并且变换动画也应该适用。

我的方法是克隆被点击的元素,
然后设置初始样式(顶部和左侧,宽度和高度等)与相同原始元素
添加类将位置设置为零,并完全展开。 width:100vw and height:100vh, top:0 left:0, position:fixed(class .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 在 sn-p 中不起作用

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>
    


【问题讨论】:

    标签: javascript html css


    【解决方案1】:

    我创立了它:
    在您的 CSS 中搜索您的图片,例如:
    #form .el_img .fullscreen
    这会在 #form 中搜索 .el_img 中的元素,并且它们有一个 fullsceen 类。
    但不是#form 中同时具有el_imgfullscreen 类的元素。 所以你需要从你的选择中删除.el_img之前的空格,所以它看起来像这样:

    #form .el_img.fullscreen {
     /* Rest of your code */
    

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

    【讨论】:

    【解决方案2】:

    我只是把它变成了谷歌照片。 通过添加一些 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>

    【讨论】:

    • 其实我不是这个意思,但这东西看起来也很酷!真的谢谢!!! :D
    猜你喜欢
    • 2020-12-16
    • 2021-10-24
    • 2018-10-12
    • 2014-02-01
    • 2014-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多