当元素滚动到视口的垂直中间时,如何执行某些操作?

时间:2014-10-24 00:52:51

标签: javascript jquery html css video

我正在尝试创建像this page这样的视频突出显示功能。当每个视频滚动到页面的中心时,该视频将突出显示。但是,不是右侧边栏上的网站地图内容,每个视频的标题/说明都会突出显示并垂直居中。像这样:

enter image description here

enter image description here

enter image description here

这是我到目前为止所拥有的:

http://jsfiddle.net/annablabber/gw9yoq06/4/

HTML

<div class="videos-container">
  <ul class="video-info">
    <li><div class="title">Title of video</div><div class="description">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div></li>
    <li><div class="title">Title of video</div><div class="description">Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</div></li>
    <li><div class="title">Title of video</div><div class="description">Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.</div></li>
    <li><div class="title">Title of video</div><div class="description">Excepteur sint occaecat cupidatat non proident.</div></li>
    <li><div class="title">Title of video</div><div class="description">sunt in culpa qui officia deserunt mollit anim id est laborum.</div></li>
    <li><div class="title">Title of video</div><div class="description">Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium.</div></li>
    <li><div class="title">Title of video</div><div class="description">Totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo.</div></li>
    <li><div class="title">Title of video</div><div class="description">Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt.</div></li>
    <li><div class="title">Title of video</div><div class="description">Neque porro quisquam est.</div></li>
    <li><div class="title">Title of video</div><div class="description">Qui dolorem ipsum quia dolor sit amet, consectetur.</div></li>
  </ul>

  <div class="videos">
    <div class="video-container"><video poster="http://graphics8.nytimes.com/newsgraphics/2013/07/21/silk-road/assets/media/driveby_970.jpg" preload="auto" src="http://graphics8.nytimes.com/newsgraphics/2013/07/21/silk-road/assets/media/driveby_970.mp4" loop></video></div>
    <div class="video-container"><video poster="http://graphics8.nytimes.com/newsgraphics/2013/07/21/silk-road/assets/media/driveby_970.jpg" preload="auto" src="http://graphics8.nytimes.com/newsgraphics/2013/07/21/silk-road/assets/media/driveby_970.mp4" loop></video></div>
    <div class="video-container"><video poster="http://graphics8.nytimes.com/newsgraphics/2013/07/21/silk-road/assets/media/driveby_970.jpg" preload="auto" src="http://graphics8.nytimes.com/newsgraphics/2013/07/21/silk-road/assets/media/driveby_970.mp4" loop></video></div>
    <div class="video-container"><video poster="http://graphics8.nytimes.com/newsgraphics/2013/07/21/silk-road/assets/media/driveby_970.jpg" preload="auto" src="http://graphics8.nytimes.com/newsgraphics/2013/07/21/silk-road/assets/media/driveby_970.mp4"></video></div>
    <div class="video-container"><video poster="http://graphics8.nytimes.com/newsgraphics/2013/07/21/silk-road/assets/media/driveby_970.jpg" preload="auto" src="http://graphics8.nytimes.com/newsgraphics/2013/07/21/silk-road/assets/media/driveby_970.mp4"></video></div>
    <div class="video-container"><video poster="http://graphics8.nytimes.com/newsgraphics/2013/07/21/silk-road/assets/media/driveby_970.jpg" preload="auto" src="http://graphics8.nytimes.com/newsgraphics/2013/07/21/silk-road/assets/media/driveby_970.mp4"></video></div>
    <div class="video-container"><video poster="http://graphics8.nytimes.com/newsgraphics/2013/07/21/silk-road/assets/media/driveby_970.jpg" preload="auto" src="http://graphics8.nytimes.com/newsgraphics/2013/07/21/silk-road/assets/media/driveby_970.mp4"></video></div>
    <div class="video-container"><video poster="http://graphics8.nytimes.com/newsgraphics/2013/07/21/silk-road/assets/media/driveby_970.jpg" preload="auto" src="http://graphics8.nytimes.com/newsgraphics/2013/07/21/silk-road/assets/media/driveby_970.mp4"></video></div>
    <div class="video-container"><video poster="http://graphics8.nytimes.com/newsgraphics/2013/07/21/silk-road/assets/media/driveby_970.jpg" preload="auto" src="http://graphics8.nytimes.com/newsgraphics/2013/07/21/silk-road/assets/media/driveby_970.mp4"></video></div>
    <div class="video-container"><video poster="http://graphics8.nytimes.com/newsgraphics/2013/07/21/silk-road/assets/media/driveby_970.jpg" preload="auto" src="http://graphics8.nytimes.com/newsgraphics/2013/07/21/silk-road/assets/media/driveby_970.mp4"></video></div>
  </div>
</div>

CSS

/**
 * Eric Meyer's Reset CSS v2.0 (http://meyerweb.com/eric/tools/css/reset/)
 * http://cssreset.com
 */
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed, 
figure, figcaption, footer, header, hgroup, 
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
    margin: 0;
    padding: 0;
    border: 0;
    font-size: 100%;
    font: inherit;
    vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section {
    display: block;
}
body {
    line-height: 1;
}
ol, ul {
    list-style: none;
}
blockquote, q {
    quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
    content: '';
    content: none;
}
table {
    border-collapse: collapse;
    border-spacing: 0;
}

/* End CSS reset */

body {
  height: 100%;
}

.videos-container {
  width: 400px;
  position: relative;
  border: 1px solid red;
}


.video-container {
  position: relative;
  width: 400px;
  height: 300px;
  margin-bottom: 33px;
  background-color: #000;
}

video {
  width: 100%;
  opacity: .4;
  -moz-transition: opacity .5s;
  -webkit-transition: opacity .5s;
  -o-transition: opacity .5s;
  transiton: opacity .5s;
}

video.highlighted {
  opacity: 1;
}

.video-info {
  position: fixed;
  z-index: 99999;
  top: 0;
  right: 0;
  width: 240px;
}

.video-info li {
  opacity: .7;
  margin-bottom: 27px;
}

.video-info li .title {
  font-size: 18px;
}

.video-info li .description {
  font-size: 16px;
  line-height: 180%;
  letter-spacing: .04em;
  display: none;
}

.video-info li.highlighted {
  margin-bottom: 56px;
  opacity: 1;
}

.video-info li.highlighted .title  {
  font-size: 28px;
  font-weight: 500;
  margin-bottom: 16px;
  letter-spacing: .03em;
}

.video-info li.highlighted .description  {
  display: block;
}

的JavaScript / jQuery的

function isScrolledIntoView(elem)
{
  var docViewTop = $(window).scrollTop();
  var docViewBottom = docViewTop + $(window).height();
  var elemTop = $(elem).offset().top;
  var elemBottom = elemTop + $(elem).height();
  return ((docViewTop < elemTop) && (docViewBottom > elemBottom));
}

$(window).scroll(function() {
  $('video').each(function() {
    if (isScrolledIntoView(this)) {

      // Find out which video it is
      var currentIndex = $('video').index(this); 
      // Highlight the associated video title/description
      $(".video-info li:eq(" + currentIndex + ")").addClass("highlighted").attr('id', 'rectangle'); 

      // Vertically center highlighted video title/description
      var viewportCenter = $(window).height() / 2;
      var rect = document.getElementById("rectangle").getBoundingClientRect();
      var liCenter = rect.top + $("li.highlighted").height()/2;
      var verticalDifference = viewportCenter - liCenter;
      $(".video-info").css("-webkit-transform", "translateY(" + verticalDifference + "px)");

      // Remove 'highlighted' class from other videos
      $("video").removeClass('highlighted'); 
      // Add 'highlighted' to this video 
      $(this).addClass('highlighted'); 
      // Play this video
      $(this).get(0).play(); 

    } else {
      // Remove 'highlighted' class on video 
      $(this).removeClass('highlighted');
      // Pause video  
      $(this).get(0).pause();
      // Pause video    
      var currentIndex = $('video').index(this);
      $(".video-info li:eq(" + currentIndex + ")").removeClass("highlighted");

    }
  });
});

视频突出显示和描述突出显示的CSS样式和jQuery工作得很好,但有一些问题

  • 每次滚动时,突出显示的视频标题/说明都会从原始位置上下变换为&#34; translateY&#34;以癫痫诱发的方式定位。
  • 如果您的视口很高,则一次会突出显示多个视频,因为多个视频都会滚动到视图中#34;。我想通过指定视频必须通过页面触摸不可见的垂直中心线来解决这个问题。不知道该怎么做!

关于如何调整我的代码以便

的任何想法
  1. 最靠近视口垂直中间的视频会突出显示(一次只能显示一个视频)
  2. 该视频的相关标题/说明会突出显示,
  3. 标题/说明是否移至视口的垂直中心?
  4. 提前致谢:)

0 个答案:

没有答案