关闭边栏(ESC)打开边栏(ESC)
用视频作背景运用也有一段时间了,更更改改的,像如今首页的效果那样,如下是全部的代码,有需要的可以拿去试下,哈。
html结构
因为本站视频背景作为一个模板使用,好多设置都是在后台有设置的,如视频的文件、背景色、透明度等,如果不使用后台参数的话,可以作相应的修改即是。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<div class="section-begin"> <div class="bg" style="background-color:<?php echo cs_get_option('i_fullpage_bg_color');?>"> <div class="img-bg" style="<?php if(!cs_get_option('i_fullpage_bg_switcher')):; ?>background-image:url(<?php echo cs_get_option('i_fullpage_bg');?>);<?php endif;?>"></div> <?php if(cs_get_option('i_fullpage_video_switcher')):?> <video id="bg-v" class="bg-video <?php if(cs_get_option('i_fullpage_videomargin_switcher')):?>video-margin<?php endif;?>" style="opacity:<?php echo cs_get_option('i_fullpage_video_opacity');?>" loop muted></video> <div class="sy-tip">开启声音</div> <?php endif;?> </div> <?php if(cs_get_option('i_fullpage_desc_switcher')):?> <div class="desc"> <img src="<?php echo home_url(); ?>/favicon.png" alt="" class="site-logo"> <p class="t1"><?php bloginfo('title'); ?></p> <p class="t3"><?php echo (cs_get_option('i_fullpage_txt')==''?bloginfo('description'):cs_get_option('i_fullpage_txt')); ?></p> </div> <?php endif;?> <div class="bottom"> <span class="hidden-xs">Copyright </span>© <?php echo cs_get_option('i_site_date'); ?> <?php echo bloginfo('name') ?> / 版本 V <?php echo cs_get_option('i_site_version'); ?> <span style="margin-left:5px;margin-right:5px" class="hidden-xs"> <a href="<?php echo home_url(); ?>/go?<?php echo cs_get_option("i_site_record_href"); ?>" target="_blank"><?php echo cs_get_option('i_site_record'); ?> </a> </span> </div> <div class="totop"><i class="fa fa-arrow-down"></i></div> </div> |
css样式
以下便是本站视频背景中用到的样式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
html,body{height:100%;} .section-begin { display: flex; flex-direction: column; justify-content: center; text-align: center; overflow: hidden; height: 0; margin: -70px 0 0; -moz-user-select: none; /*火狐*/ -webkit-user-select: none; /*webkit浏览器*/ -ms-user-select: none; /*IE10*/ -khtml-user-select: none; /*早期浏览器*/ user-select: none; transition:all 1.5s; opacity: 0; } .section-begin .desc{ transition:all .3s; } .section-begin .bottom{ position: fixed; bottom: 0; left: 0; width: 100%; z-index: -1; text-align: center; padding: 15px; font-size: 12px; color: #fff; transition:all .4s linear; background-color: rgba(0,0,0,.3); font-weight: normal; } .section-begin #bg-v { display: none; min-width: 100%; min-height: 100%; object-fit: cover; width: 100%; } .section-begin .video-margin{ min-height: calc(100% + 70px)!important; margin-top: -70px; } .section-begin .sy-tip{ position: absolute; right: 25px; bottom: 60px; font-size: 12px; color: #555; border-radius: 50px; cursor: pointer; font-weight: 400; background: #fff; padding: 3px 12px; display: none; letter-spacing: 2px; } .section-begin .bottom a { text-decoration: none; color: #fff; } .section-begin .bg { position: fixed; left:0; top:0; width: 100%; height: 100%; background-repeat: no-repeat; background-position: 50% 50%; background-size: cover; z-index: -1; transition: all 1.5s; overflow: hidden; } .section-begin .bg .img-bg{ width: 100%; height: 100%; position: absolute; left: 0; top:0; background-repeat: no-repeat; background-position: 50% 50%; background-size: cover; z-index: -1; transition: all 1s; overflow: hidden; opacity: 0.05; } .section-begin .site-logo { width: 120px; height: 120px; margin: 0 auto 40px; box-shadow: 0 0 30px -3px #DB214C; border-radius: 50%; opacity: 0; animation:logo-an 5s linear 0.7s forwards; animation-play-state:paused; } @keyframes logo-an { 0% {opacity: 1; transform: translate3d(0, 0, 0) rotate(0deg) scale(1);} 10% {opacity: 0.8; transform: translate3d(-100px, 0px, 0) rotate(10deg) scale(0.7);} 35% {opacity: 0.6; transform: translate3d(100px, 0px, 0) rotate(30deg) scale(0.4);} 50% {opacity: 0.4; transform: translate3d(0, 0, 0) rotate(360deg) scale(0);} 80% {opacity: 0.2; transform: translate3d(0, 0, 0) rotate(720deg) scale(1);} 90% {opacity: 0.1; transform: translate3d(0, 0, 0) rotate(3600deg) scale(6);} 100% {opacity: 1; transform: translate3d(0, 0, 0) rotate(3600deg) scale(1);} } /*三种标题样式*/ .section-begin{ font-family: "Microsoft YaHei","STHeiti","WenQuanYi Micro Hei",SimSun,sans-serif; font-weight: 200; } .section-begin .t1 { font-size: 60px; line-height: 100px; background: url('<?php echo get_template_directory_uri()?>/fullpage/titleline.png') no-repeat center bottom; transform:scale(0.5); transition: all 1s; opacity: 0; margin: 0; padding: 0; color: #fff; } .section-begin.load .t1{ transform: scale(1); transition-duration: 1s; transition-delay: 0.7s; opacity: 1; } .section-begin .t2 { opacity: 0; font-size: 40px; line-height: 75px; transform:scale(1.5); transition: all 1s; margin: 0; padding: 0; color: #fff; } .section-begin.load .t2{ transform: scale(1); transition-duration: 1s; transition-delay: 1s; opacity: 1; } .section-begin .t3 { margin: 0; padding: 0; font-size: 24px; line-height: 48px; margin-top: 20px; opacity: 0; transition: all 1s; transform: translateY(-20px); color: #fff; } .section-begin.load .t3{ transform:translateY(0); transition-duration: 1s; transition-delay: 1.3s; opacity: 1; } .section-begin .totop { position: absolute; bottom: 6px; right: 25px; z-index: 1; } .section-begin .totop i{ background-color: transparent; padding: 10px; border-radius: 50%; outline: 0px; font-size: 14px; transition: all .5s; cursor: pointer; color: #fff; } .section-begin .totop i:hover{ background-color: #fff; color: #333!important; } @media (min-width:1200px){ .section-begin #bg-v,.section-begin .sy-tip{ display: block; } } @media(max-width:1199px){ .section-begin .bg .img-bg{ background-image:url(<?php echo cs_get_option('i_fullpage_bg');?>); } } @media(min-width:768px){ .section-begin-h{height: calc(100% + 70px);opacity:1;} } @media(max-width:767px){ .section-begin { margin: -52px 0 0; height: calc(100% + 52px); opacity:1; } .section-begin .totop{ right: 9px; } } @media(max-height:480px){ .section-begin .site-logo { width: 80px; height: 80px; margin-bottom: 15px; } .section-begin .t1 { font-size: 24px; line-height: 50px; background-size: 60vw; } .section-begin .t2 { font-size: 20px; line-height: 40px; } .section-begin .t3 { font-size: 18px; line-height: 20px; } } @media(max-width:567px){ .section-begin .site-logo { width: 60px; height: 60px; margin-bottom: 15px; } .section-begin .t1 { font-size: 24px; line-height: 50px; background-size: 70vw; } .section-begin .t2 { font-size: 20px; line-height: 50px; } .section-begin .t3 { font-size: 16px; line-height: 10px; } } |
JS代码
这里主要是处理视频的播放与加载,声音的开关及滚动整屏后视频会暂停播放。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
if($('#bg-v').length>0&&$(window).width()>=1200){ var myVid = document.getElementById("bg-v"); $('#bg-v').attr('src',videofile); $('#bg-v').attr('data-autoplay',''); myVid.play(); //声音操作 $('.section-begin').click(function(){ if(myVid.muted===false){ myVid.muted=true; $('.sy-tip').text('开启声音'); }else{ myVid.muted=false; $('.sy-tip').text('关闭声音'); } }); } $('.section-begin .totop').click(function(){ $('body,html').animate({scrollTop:$('.section-begin').outerHeight()},700); return false; }) |
加载完内容后,添加显示视频层,cookie等的JS
1 2 3 4 5 6 7 8 9 10 11 12 |
window.onload = function () { $('.site-logo').css('animation-play-state','running'); $('.section-begin').addClass('load section-begin-h'); $('.section-begin .bg .img-bg').css('opacity', '1'); if($('.section-begin').length>0){ var myVid = document.getElementById("bg-v"); if($(window).width()>=1200){myVid.play();} var exp = new Date(); exp.setTime(exp.getTime() + 60 * 1000*20); document.cookie="section-begin=load;expires=" + exp.toGMTString() } }; |
滚动完整个视频后,将暂停播放
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
scrollTop = $(document).scrollTop(); if($('.section-begin').length>0){ if(scrollTop>0){ $('.section-begin .desc').css('opacity',5/scrollTop); }else{ $('.section-begin .desc').css('opacity',1); } if($('.section-begin #bg-v').length>0){ var myVid = document.getElementById("bg-v"); if(scrollTop>=$('.section-begin').outerHeight()){ if($(window).width()>=1200){myVid.pause()}; }else{ if($(window).width()>=1200){myVid.play()}; } } } |
效果动画如下:
其中:
videofile
,是视频的地址,感觉有点乱,代码在上面了,有时间优化的话应该会更好
你好,用了感觉挺好的,我是单独做了一个页面,之后再点击跳转进入网站的。
就是有个问题,在手机上无法显示视频出来。想说可以帮忙看一下吗?
网址:https://guest997.ml/(视频显示可能会有点慢)
谢谢了!
站长,我第一次看到wordpress页面很大气,请问这个视频安装有方法嘛?我还是技术小白
这个有文章介绍的,你在本站找找,有人问过,所以有写过
好看。收藏一下,空了学习学习,最近都没时间弄这个了。
好久没来了,自那次不告而别,很多话来不及说。没想到今日一看,老哥的博客是越来越风光了,文章也写的很好,受益良多,老哥坚持下去,特别棒,我以后会常来的——山西的朋友(xiaopan)

哈哈 感谢来看看
变成全屏了,突然还不习惯
嗯 有点懵了
我试试
首页改的更好看了咯,不停在折腾,我都荒了好久了
不停的改呀改,你的我常去看都没怎么更新了,工作忙吧