当前位置:网站首页>Simple rotation map and hamster beating
Simple rotation map and hamster beating
2022-07-25 16:01:00 【Gai Yuexi's boyfriend outside the circle (kuaixi)】
Catalog
Shuffling figure
demand
There are still no living interns ...... So write a simple rotation chart to play . The specific requirements are , Use display:none Hide picture , Realize the rotation of pictures .
Ideas
structure
The specific structure is , Put several pictures in the parent container , These pictures can be arranged horizontally or vertically ( Because what's used here is display Realization , If you use the positioning implementation, you need to pay attention to the horizontal or vertical ). There is only one picture at a time display by block, The rest are none, In this way, only one picture can be displayed . The width and height of the image are the same as that of the parent container , And set the parent container overflow:hidden( Again , the reason being that display Realization , So you don't need to set this , But in order to load normally , The setting is better ).
This is it. html and css Basic structure . Then the rotation effect needs the help of JavaScript Realization , Be sure to use a timer . In order to show only one picture at the same time , We need a variable index Record the number of pictures currently displayed . Encapsulate a method , Only the second index A picture , And make index Increasing , Then use the timer , Call this method every once in a while .
The function of the basic rotation chart is realized in this way .
You can also add two buttons on both sides of the picture , As a button to display the previous and next . When you click the previous button , Clear the timer first , To avoid confusion , And then modify index And let No index Pictures show , Restart the timer . The logic of the next button is similar .
Code implementation
html part
<!-- container Used to hold pictures -->
<div class="container">
<img src="pictures/1.jpg" class="picture">
<img src="pictures/2.jpg" class="picture">
<img src="pictures/3.jpg" class="picture">
<img src="pictures/4.jpg" class="picture">
<img src="pictures/5.jpg" class="picture">
<img src="pictures/6.jpg" class="picture">
<div class="btn last"> On </div>
<div class="btn next"> Next </div>
</div>PS: The pictures are all gaiyuexi photos , The cover is so beautiful, woo woo ......
css part
.container {
display: flex;
width: 400px;
height: 400px;
overflow-x: hidden;
margin: 200px auto;
/* Relative positioning */
position: relative;
}
.container .picture {
width: 400px;
height: 400px;
}
.container .btn {
position: absolute;
height: 30px;
width: 30px;
line-height: 30px;
text-align: center;
background-color: aliceblue;
border-radius: 50%;
cursor: pointer;
}
.container .last {
left: 20px;
top: 50%;
transform: translateY(-50%);
}
.container .next {
right: 20px;
top: 50%;
transform: translateY(-50%);
}JavaScript part
// Which picture is currently displayed
let index = 0;
// Get all pictures
let img_list = document.getElementsByClassName('picture')
// display picture
function showImg() {
// Now index Increasing , This will guarantee index It's really the picture being displayed
index = (index + 1) % 6
// Except for index Zhang Wai all display:none
for (let i = 0; i < img_list.length; i++) {
img_list[i].style.display = "none"
}
img_list[index].style.display = "block"
}
// Turn on timer
let timer = setInterval(showImg, 1500);
// Click the previous button
function last() {
clearInterval(timer)
if (index === 0) {
index = 5
} else {
index--
}
// Except for index Zhang Wai all display:none
for (let i = 0; i < img_list.length; i++) {
img_list[i].style.display = "none"
}
img_list[index].style.display = "block"
timer = setInterval(showImg, 1500);
}
// Click on the next picture
function next() {
clearInterval(timer)
index = (index + 1) % 6
// Except for index Zhang Wai all display:none
for (let i = 0; i < img_list.length; i++) {
img_list[i].style.display = "none"
}
img_list[index].style.display = "block"
timer = setInterval(showImg, 1500);
}
let btns = document.getElementsByClassName('btn')
btns[0].onclick = last;
btns[1].onclick = next;Realization effect
Carousel presentation
summary
This article is just for simple use display Property implements the rotation graph , The effect is not better positioned , Do it later when you have time .
Whac-A-Mole
demand
Continue without incident ...... Write a play with a hamster .
Thought analysis
structure
The structure is actually very simple . I designed a nine square , Gopher pictures appear randomly in each Jiugong grid . The specific implementation method is , Parent element div Contained in the 9 individual div, And open the parent element BFC, Facilitate the floating of sub elements . All child elements float to the left , And set up border、width、height, Three per line , Form a nine palace grid . Every subelement is item class , Set some uniform styles . There are sub elements of beating hamsters , Set to mouse class .
That's it HTML and css, It's time to design JavaScript.
The logic is , Turn on timer , Each time it produces something different from the last 0-8 The random number position, Then set No position The child element is mouse、item class , The rest is just item class . then , For each item Are bound to click event callback functions , Judge event Target element , namely event.target, its class Is there a mouse. If there is mouse, Then hit and score .
It's not hard to think , You can write like clouds and flowing water .
Code implementation
html part
<div class="count_wrapper">0 branch </div>
<div class="container">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>stay container Place nine child elements in .
css part
.count_wrapper {
width: 100px;
height: 100px;
margin: 0 auto;
line-height: 100px;
text-align: center;
font-size: 30px;
}
.container {
width: 600px;
height: 600px;
margin: 0 auto;
background-color: antiquewhite;
/* Turn on BFC */
overflow: hidden;
}
.container .item {
float: left;
border: 1px solid rgb(211, 209, 209);
width: 198px;
height: 198px;
}
/* Show gopher item */
.mouse {
cursor: pointer;
background-image: url('./pictures/mouse.jpg');
background-size: cover;
background-position: center;
}JavaScript part
// Randomly display the place of gophers
let position = 0
// All positions
let mouses = document.getElementsByClassName('item')
// The current score
let count = 0;
// Scoring elements
let count_wrapper = document.getElementsByClassName('count_wrapper')[0]
// Show gophers
function showMouse() {
for (let i = 0; i < mouses.length; i++) {
mouses[i].className = 'item'
}
mouses[position].className += " mouse"
}
// Randomly display gophers
function randomShowMouse() {
let new_position = Math.floor(Math.random() * 9)
// Find new random points
while (new_position == position) {
new_position = Math.floor(Math.random() * 9)
}
position = new_position
// Show gophers
showMouse()
}
setInterval(randomShowMouse, 600);
let items = document.getElementsByClassName('item')
for (let i in items) {
items[i].onclick = function (e) {
if (e.target.className.indexOf('mouse') > -1) {
count++
count_wrapper.innerText = count + ' branch '
}
}
}Realization effect
Groundhog demonstration
summary
Beating the hamster is a simple and interesting game , It's easy to implement .
边栏推荐
- 【IJCAI 2022】参数高效的大模型稀疏训练方法,大幅减少稀疏训练所需资源
- mysql 表写锁
- 用GaussDB(for Redis)存画像,推荐业务轻松降本60%
- Understanding of this object
- Ml image depth learning and convolution neural network
- Mysql读写锁
- How to realize page inclusion
- Geogle colab notes 1-- run the.Py file on the cloud hard disk of Geogle
- Distributed | practice: smoothly migrate business from MYCAT to dble
- Endnote add Chinese gbt7714 style how to quote documents in word
猜你喜欢

推荐系统-协同过滤在Spark中的实现

LeetCode - 359 日志速率限制器 (设计)

How matlab produces random complex sequences

Why is preparestatement better and safer?

Leetcode - 362 knock counter (Design)

Leetcode - 622 design cycle queue (Design)

开发者如何为React Native选择合适的数据库

【IJCAI 2022】参数高效的大模型稀疏训练方法,大幅减少稀疏训练所需资源

Experimental reproduction of image classification (reasoning only) based on caffe resnet-50 network

Geogle colab notes 1-- run the.Py file on the cloud hard disk of Geogle
随机推荐
Alibaba's internal "100 billion level concurrent system architecture design notes" are all inclusive, too comprehensive
MATLAB optimization tool manopt installation
Activity review | July 6 Anyuan AI X machine heart series lecture No. 2 | MIT professor Max tegmark shares "symbiotic evolution of human and AI"
Leetcode - 303 area and retrieval - array immutable (design prefix and array)
MySQL页锁
产品动态丨Android 13 高效适配全新升级
I interviewed 8 companies and got 5 offers in a week. Share my experience
ML - Speech - traditional speech model
Window system black window redis error 20creating server TCP listening socket *: 6379: listen: unknown error19-07-28
权限管理-角色分配菜单
Endnote cannot edit range resolution
MySQL教程65-MySQL操作表中数据
TypeError: Unrecognized value type: <class ‘str‘> ParserError: Unknown string format
MySQL教程67-使用DISTINCT过滤重复数据
Distributed | practice: smoothly migrate business from MYCAT to dble
Implementation of recommendation system collaborative filtering in spark
30行自己写并发工具类(Semaphore, CyclicBarrier, CountDownLatch)
物理防火墙是什么?有什么作用?
R语言偏相关性计算(Partial Correlation)、使用ggm包的pcor函数计算偏相关性(Partial Correlations)
Sword finger offer | number of 1 in binary