0


原神5.1前端网页HTML+CSS+JS

这篇文章主要是总结一下我在制作页面的时候用到的一些技术以及经验总结,博主也是第一次写网页,代码也是在不断地“进化”,哪里写的不好大家可以随意指出。

下面就是一些经验总结,如果想看具体效果我这里也不好展示,需要的可以看b站上的视频:

HTML+CSS+JS试做原神5.1前端网页_哔哩哔哩_bilibili,不想看我唠嗑的直接看最后我会放网盘链接。

1.布局(开头)

我们很多人刚学完HTML+CSS就非常手痒想要做一下自己的网页,但刚开始发现不知道该怎么上手,也就是最开始的布局不知道怎么写比较好。这里我主要讲一下两种情况:

记得最开始放一段代码(可以有效删除元素的内外边距,有助于后面布局):

  1. * {
  2. margin: 0;
  3. padding: 0;
  4. }

** 1.普通页面,也就是会出现滚轮条,可以直接上下拉动**

这种界面没什么好说的,直接无脑往上放div即可,如果你还是不放心,我们可以指定

  1. overflow-x: hidden;

意思就是说我们的横向如果有内容超出了,就不会显示,然后指定一个具体的宽度,一般屏幕的默认大小是1912×954,高度可以不用管。然后就可以div盒子往里面加采用一般的流式布局即可。

2.整块滑动的界面(没有滚动条,上下滑动时一整块界面一起动)

例如这样:

8c8250cf53da44168df3952009ee430b.png

这边我的思路是这样的:我们对整个界面分块处理,用一个大盒子包裹

618af7ef7e18463596e358a02e8d30e8.png

也就是直接设置上图box的高度,里面的盒子全部采用定位的布局方式,然后我们直接指定盒子的宽高,最后只需要使用js在每次滑动界面的时候,修改每个和盒子的top即可(大概就是这样)

  1. elements.forEach(function (elementId) {
  2. document.getElementById(elementId).addEventListener('mousewheel', function (e) {
  3. e = e || window.event;
  4. if (!canScroll)
  5. return;
  6. canScroll = false;
  7. setTimeout(function () {
  8. canScroll = true;
  9. }, 500); // 设置响应时间间隔,这里是1秒
  10. if (e.wheelDelta > 0) { //当滑轮向上滚动时
  11. console.log("滑轮向上滚动" + num);
  12. if (num == 5 || yunxugundong == false) {
  13. return;
  14. }
  15. else {
  16. num = num + 1;
  17. $(".header").animate({
  18. top: '+=954px',
  19. });
  20. $(".bg2").animate({
  21. top: '+=954px',
  22. });
  23. $(".bg3").animate({
  24. top: '+=954px',
  25. });
  26. $(".bg4").animate({
  27. top: '+=954px',
  28. });
  29. $(".bg5").animate({
  30. top: '+=954px',
  31. });
  32. $(".bg6").animate({
  33. top: '+=954px',
  34. });
  35. }
  36. }
  37. if (e.wheelDelta < 0) { //当滑轮向下滚动时
  38. console.log("滑轮向下滚动" + num);
  39. if (num <= 0 || yunxugundong == false) {
  40. if (yunxugundong == false)
  41. return;
  42. else if (num <= 0) {
  43. const element = document.querySelector('.bg6');
  44. element.style.animation = 'bounce 0.5s ease alternate';
  45. setTimeout(function () {
  46. element.style.animation = '';
  47. }, 500);
  48. }
  49. }
  50. else {
  51. num = num - 1;
  52. $(".header").animate({
  53. top: '-=954px',
  54. });
  55. $(".bg2").animate({
  56. top: '-=954px',
  57. });
  58. $(".bg3").animate({
  59. top: '-=954px',
  60. });
  61. $(".bg4").animate({
  62. top: '-=954px',
  63. });
  64. $(".bg5").animate({
  65. top: '-=954px',
  66. });
  67. $(".bg6").animate({
  68. top: '-=954px',
  69. });
  70. }
  71. }
  72. });
  73. });

,如果是横向的也可以这么做,只需要指定宽度变化即可,虽然代码比较史但是胜在好理解,改起来也比较方便。

2.背景以及图片布置

界面中有很多地方都是图片,我的建议是没有特殊的要求全都布置成div容器的backgrou-image,这样子比较方便管理也比较简单,直接加入图片然后指定图片的宽高全部都是100%,也就是铺满整个容器,这样子直接通过div容器就可以修改图片大小,除非是有特殊的需求,再将img容器放到div容器里面,但是这样子就需要设置img容器的布局方式以及具体位置,还是比较麻烦的,比较适合处理相对复杂的情况。

3.一些动画效果的实现原理

1.出现黑色背景里面放一张图片

such as:

701d064da87f41faaa2bee3a2e56fbaa.png

这个实际上非常简单,就是你提前先布置好一个黑色背景并且设置好透明度,然后往里面提前写好东西,再把它隐藏了,当你点击某个按钮的时候就会直接修改display: none;就会直接显现。同时我们可以再js里面设置点击整个黑色背景的时候就消失,这样同时解决了我们打开这个额外的界面的时候点击除了视频以外的部分就会直接关闭的效果。

这里有几点需要注意:

1.我们在打开界面的时候滚轮是不能动的,因此我多设置了一个变量来约束滚轮是否会执行上下滚动(js代码里面有).

2.重新打开视频的时候视频会重新播放,这就需要我们在关闭或者开启的时候将视频重新归0,值得注意的是我们在关闭界面的时候一定要暂停视频,否则视频依旧会继续播放.

3.这个额外的界面我的建议是写在bg2盒子的第一个子级位置,such as:

  1. <div class="bg2" id="bg2">
  2. <div class="bg2-box">
  3. <div class="img1" id="bg2-bg2-box-img1" onmouseover="changeImage2()" onmouseout="resetImage2()"><img
  4. src="../image/huache1.webp" id="bg2-img1" style="width: 335.47px;height: 255.77px;"></div>
  5. <div class="img2" id="bg2-bg2-box-img2" onmouseover="changeImage3()" onmouseout="resetImage3()"><img
  6. src="../image/mengyuxianshi.webp" id="bg2-img2" style="width: 335.47px;height: 255.77px;"></div>
  7. <div class="img3" id="bg2-bg2-box-img3" onmouseover="changeImage4()" onmouseout="resetImage4()"><img
  8. src="../image/qishi.webp" id="bg2-img3" style="width: 335.47px;height: 255.77px;"></div>
  9. </div>
  10. <div class="bg2-blackground1">
  11. <div class="huache-video"></div>
  12. <div class="huache-video-border"></div>
  13. <div class="huache-video-footer"></div>
  14. <div class="video1">
  15. <video src="../image/huache-video.mp4" autoplay muted loop
  16. style="width: 100%; height: 100%;"></video>
  17. </div>
  18. </div>
  19. <div class="bg2-blackground2">
  20. <div class="mengyuxianshi-video-header"></div>
  21. <div class="huache-video-border"></div>
  22. <div class="mengyuxianshi-video-footer"></div>
  23. <div class="video1">
  24. <video src="../image/mengyuxianshi.mp4" autoplay muted loop
  25. style="width: 100%; height: 100%;"></video>
  26. </div>
  27. </div>
  28. <div class="bg2-blackground3">
  29. <div class="qishi-video-header"></div>
  30. <div class="huache-video-border"></div>
  31. <div class="qishi-video-footer"></div>
  32. <div class="video1">
  33. <video src="../image/qishi.mp4" autoplay muted loop style="width: 100%; height: 100%;"></video>
  34. </div>
  35. </div>
  36. </div>

跟其它盒子平级,这样做的优点是什么呢,就是我们可以随时修改z-index来保证这个界面一定是在所有子级的上面.

(下面是举例代码)

  1. <div class="bg2-blackground3">
  2. <div class="qishi-video-header"></div>
  3. <div class="huache-video-border"></div>
  4. <div class="qishi-video-footer"></div>
  5. <div class="video1">
  6. <video src="../image/qishi.mp4" autoplay muted loop style="width: 100%; height: 100%;"></video>
  7. </div>
  8. </div>

css代码:

  1. .bg2 .bg2-blackground3 {
  2. z-index: 100;
  3. position: absolute;
  4. top: 0;
  5. left: 0px;
  6. width: 2208px;
  7. height: 954px;
  8. background-color: rgba(0, 0, 0, 0.7);
  9. display: none;
  10. }

js代码:

  1. $(".bg2 .img3").click(function () {
  2. $(".bg2 .bg2-blackground3 .video1 video").get(0).currentTime = 0;
  3. $(".bg2 .bg2-blackground3 .video1 video").trigger("play");
  4. $(".bg2 .bg2-blackground3").show();
  5. yunxugundong = false;
  6. })
  7. $(".bg2 .bg2-blackground3").click(function () {
  8. $(".bg2 .bg2-blackground3 .video1 video").trigger("pause");
  9. $(".bg2 .bg2-blackground3").hide();
  10. yunxugundong = true;
  11. })

2.模块化设计

举个例子,当出现这一类情况:

3dd5bb976c134512ac2d10ee1d9fd529.png

除了下面的按钮以及上面的角色情报这个标题不用变化,是不是其它都需要变化,那么我们就可以把上面的人物简介以及右边的人物立绘设置成一个容器,通过js点击不同按钮的时候直接修改其中的图片即可,可以直接去看我里面的代码实现.

3.轮播图

虽然网上有很多控件都可以实现轮播图,但是我们也必须要学会怎么手写轮播图,这样以便我们可以实现更加复杂的效果:

084b5118db6e450e95044208fb4f75f4.png

原理也很简单,就是提前设置好所需要的容器,然后设置好图片的样式,最后通过js实现(我靠我这说了跟没说一样,其中的核心是css样式的设计)

献上代码(可以直接作为模板套用):

html

  1. <div class="bg6" id="bg6">
  2. <div class="bg6-header"></div>
  3. <div class="bg6-wrap">
  4. <div class=" swipe1"></div>
  5. <div class=" swipe2"></div>
  6. <div class=" swipe3"></div>
  7. <div class=" swipe4"></div>
  8. <div class=" swipe5"></div>
  9. <div class=" swipe6"></div>
  10. <div class=" swipe7"></div>
  11. </div>
  12. <div class="turn">
  13. <div class="prev">
  14. <img src="../image/bg6-left.png" alt="">
  15. <img src="../image/bg6-left2.png" alt="">
  16. </div>
  17. <div class="next">
  18. <img src="../image/bg6-right.png" alt="">
  19. <img src="../image/bg6-right2.png" alt="">
  20. </div>
  21. </div>
  22. <ul class="swipe-index">
  23. <li>
  24. <img src="../image/button2.png" alt="">
  25. <img src="../image/button1.png" alt="">
  26. </li>
  27. <li>
  28. <img src="../image/button2.png" alt="">
  29. <img src="../image/button1.png" alt="">
  30. </li>
  31. <li>
  32. <img src="../image/button2.png" alt="">
  33. <img src="../image/button1.png" alt="">
  34. </li>
  35. <li>
  36. <img src="../image/button2.png" alt="">
  37. <img src="../image/button1.png" alt="">
  38. </li>
  39. <li>
  40. <img src="../image/button2.png" alt="">
  41. <img src="../image/button1.png" alt="">
  42. </li>
  43. <li>
  44. <img src="../image/button2.png" alt="">
  45. <img src="../image/button1.png" alt="">
  46. </li>
  47. <li>
  48. <img src="../image/button2.png" alt="">
  49. <img src="../image/button1.png" alt="">
  50. </li>
  51. </ul>
  52. <ul class="img-index">
  53. <li>
  54. <img src="../image/weibo.png" alt="">
  55. </li>
  56. <li>
  57. <img src="../image/weixin.png" alt="">
  58. </li>
  59. <li>
  60. <img src="../image/bili.png" alt="">
  61. </li>
  62. <li>
  63. <img src="../image/mhy.png" alt="">
  64. </li>
  65. </ul>
  66. <div class="bg6-blackground">
  67. <div class="weixinjietu">
  68. <img src="../image/weixin-erweima.webp" alt="">
  69. </div>
  70. <div class="bg6-text">- 官方微信号 -</div>
  71. </div>
  72. </div>

css:

  1. .bg6-wrap {
  2. position: absolute;
  3. top: 20%;
  4. width: 100%;
  5. height: 466px;
  6. font-size: 89.7222px;
  7. }
  8. .bg6-wrap div {
  9. position: absolute;
  10. left: calc(50% - 4.845em);
  11. width: 9.69em;
  12. height: 5.46em;
  13. transition: all .4s;
  14. background-size: 100% 100%;
  15. }
  16. .bg6-wrap div:nth-child(1) {
  17. background-image: url(../image/bg6-1.png);
  18. }
  19. .bg6-wrap div:nth-child(2) {
  20. background-image: url(../image/bg6-2.png);
  21. }
  22. .bg6-wrap div:nth-child(3) {
  23. background-image: url(../image/bg6-3.png);
  24. }
  25. .bg6-wrap div:nth-child(4) {
  26. background-image: url(../image/bg6-4.png);
  27. }
  28. .bg6-wrap div:nth-child(5) {
  29. background-image: url(../image/bg6-5.png);
  30. }
  31. .bg6-wrap div:nth-child(6) {
  32. background-image: url(../image/bg6-6.png);
  33. }
  34. .bg6-wrap div:nth-child(7) {
  35. background-image: url(../image/bg6-7.png);
  36. }
  37. .swipe1 {
  38. z-index: 2;
  39. transform: translateX(0) scale(1);
  40. opacity: 1;
  41. }
  42. .swipe2 {
  43. z-index: 1;
  44. transform: translateX(3em) scale(0.7);
  45. opacity: 1;
  46. }
  47. .swipe3 {
  48. z-index: 0;
  49. transform: translateX(3em) scale(0.4);
  50. opacity: 0;
  51. }
  52. .swipe4 {
  53. z-index: 0;
  54. transform: translateX(0) scale(1);
  55. opacity: 0;
  56. }
  57. .swipe5 {
  58. z-index: 0;
  59. transform: translateX(-3em) scale(0.4);
  60. opacity: 0;
  61. }
  62. .swipe6 {
  63. z-index: 0;
  64. transform: translateX(-3em) scale(0.4);
  65. opacity: 0;
  66. }
  67. .swipe7 {
  68. z-index: 1;
  69. transform: translateX(-3em) scale(0.7);
  70. opacity: 1;
  71. }
  72. .turn {
  73. z-index: 3;
  74. position: absolute;
  75. left: 22%;
  76. top: 40%;
  77. width: 1000px;
  78. height: 82.14px;
  79. display: flex;
  80. justify-content: space-between;
  81. }
  82. .prev,
  83. .next {
  84. position: relative;
  85. width: 98.05px;
  86. height: 82.14px;
  87. cursor: pointer;
  88. }
  89. .prev img,
  90. .next img {
  91. position: absolute;
  92. left: 0;
  93. top: 0;
  94. width: 100%;
  95. height: 100%;
  96. }
  97. .prev img:nth-child(2),
  98. .next img:nth-child(2) {
  99. display: none;
  100. }
  101. .swipe-index {
  102. position: absolute;
  103. top: 70%;
  104. width: 100%;
  105. height: 28.7px;
  106. display: flex;
  107. justify-content: center;
  108. }
  109. ul {
  110. list-style: none;
  111. }
  112. .swipe-index li {
  113. position: relative;
  114. width: 32px;
  115. height: 28.7px;
  116. margin-left: 30px;
  117. }
  118. .swipe-index li img {
  119. position: absolute;
  120. top: 0;
  121. left: 0;
  122. width: auto;
  123. height: 28.7px;
  124. cursor: pointer;
  125. }
  126. .swipe-index li img:nth-of-type(2) {
  127. display: none;
  128. }
  129. .swipe-index li:first-child img:nth-of-type(2) {
  130. display: block;
  131. }
  132. .img-index {
  133. position: absolute;
  134. left: -8%;
  135. bottom: 10%;
  136. width: 100%;
  137. height: 103.6px;
  138. display: flex;
  139. justify-content: center;
  140. }
  141. .img-index li {
  142. position: relative;
  143. width: 32px;
  144. height: 28.7px;
  145. margin-left: 200px;
  146. }
  147. .img-index li img {
  148. position: absolute;
  149. top: 0;
  150. left: 0;
  151. width: auto;
  152. height: 103.6px;
  153. cursor: pointer;
  154. }

js:

  1. let swipeStyle = ["swipe1", "swipe2", "swipe3", "swipe4", "swipe5", "swipe6", "swipe7"];
  2. //游戏特色模块
  3. $(".turn").children().mouseenter(function () {
  4. $(this).children().eq(1).show()
  5. $(this).children().eq(0).hide()
  6. });
  7. $(".turn").children().mouseleave(function () {
  8. $(this).children().eq(1).hide()
  9. $(this).children().eq(0).show()
  10. });
  11. let featureAnimation = setInterval(next, 5000);
  12. $(".turn").children().click(function () {
  13. clearInterval(featureAnimation);
  14. if ($(this).attr("class") == "next") {
  15. next();
  16. } else if ($(this).attr("class") == "prev") {
  17. prev();
  18. }
  19. featureAnimation = setInterval(next, 5000);
  20. });
  21. function next() {
  22. swipeStyle.unshift(swipeStyle.pop());
  23. feature = swipeStyle.indexOf("swipe1");
  24. for (let i = 0; i < 7; i++) {
  25. $(".bg6-wrap div").eq(i).attr("class", swipeStyle[i]);
  26. if (i == feature) {
  27. $(".swipe-index li").eq(i).children().eq(1).show();
  28. } else {
  29. $(".swipe-index li").eq(i).children().eq(1).hide();
  30. }
  31. }
  32. }
  33. function prev() {
  34. swipeStyle.push(swipeStyle.shift());
  35. feature = swipeStyle.indexOf("swipe1");
  36. for (let i = 0; i < 7; i++) {
  37. $(".bg6-wrap div").eq(i).attr("class", swipeStyle[i]);
  38. if (i == feature) {
  39. $(".swipe-index li").eq(i).children().eq(1).show();
  40. } else {
  41. $(".swipe-index li").eq(i).children().eq(1).hide();
  42. }
  43. }
  44. }
  45. $(".swipe-index li").mouseenter(function () {
  46. if (feature == $(this).index()) {
  47. return;
  48. } else {
  49. $(this).children().eq(1).show();
  50. }
  51. });
  52. $(".swipe-index li").mouseleave(function () {
  53. if (feature == $(this).index()) {
  54. return;
  55. } else {
  56. $(this).children().eq(1).hide();
  57. }
  58. });
  59. $(".swipe-index li").click(function () {
  60. clearInterval(featureAnimation);
  61. if (feature == $(this).index()) {
  62. featureAnimation = setInterval(next, 5000);
  63. return;
  64. } else {
  65. let count = feature - $(this).index()
  66. console.log(count)
  67. if (count > 0) {
  68. for (let i = 0; i < count; i++) {
  69. prev();
  70. }
  71. } else {
  72. for (let i = 0; i < -count; i++) {
  73. next();
  74. }
  75. }
  76. }
  77. featureAnimation = setInterval(next, 5000);
  78. });

4.总结

反正就是说,如果想要写好一个界面就必须要上手去写,初学者可以不需要一定要遵守某些原则:例如代码写的多规范,网页布局一定要怎么样等等。网页制作本来就是一个发挥创造和想象的过程,如果说为了遵循条条框框导致自己网页布局千疮百孔那不如不要,网页制作我们又不需要看源码,只需要看到效果即可。

最后给出一点经验总结:

1.图片和装它的容器尽可能一样大。

2.别惦记着你那float了,现在是flex和position的天下,反正布局不了解遇事不决直接定位,直接上绝对定位,top、left、righ、bottom四个位置总有适合你的,大不了就一点一点量过去反正效果到位就行了。

3.多看别人的优秀代码,汲取一下别人的设计思路,前期我们初学者不需要写的有多好,例如像别人的代码就一定会有css样式复用,用到了层叠性和继承性,我们可能分析不出那么多东西。唉那我就不分析了,每写一个div我就设置一个class,这样谁也影响不了谁,注意命名别重复了就行,这边建议采取xxx-xxxx-xxx的命名方式,不容易搞混。

**ok最后献上源码:链接:https://pan.baidu.com/s/1zs28JNV-nMSSov4qrfX39g
提取码:1gl4 **

后续我会继续改进界面,加入官网的界面以及一些后端知识的运用,如果有什么地方不明白或者哪里做的不好误人子弟了大家可以随意指出,尽情地鞭策我吧!!如果觉得还行可以点个关注吗??

标签: html css javascript

本文转载自: https://blog.csdn.net/m0_73554747/article/details/142743487
版权归原作者 叶无缺 所有, 如有侵权,请联系我们删除。

“原神5.1前端网页HTML+CSS+JS”的评论:

还没有评论