当前位置:网站首页>JS implementation mouse can achieve the effect of left and right scrolling
JS implementation mouse can achieve the effect of left and right scrolling
2022-06-25 05:51:00 【The rain fell on the city with a smile】
Basic personnel with the following key knowledge are required :
master html Basic elements basic knowledge
master css Layout Basics
js Sports Basics
This example is very simple , The most important thing is to take the postgraduate entrance examination
style css
*{
padding: 0;
margin: 0;
}
li{
list-style: none;
}
img{
border:0;
}
/* control */
.control{
border-bottom: 1px solid #ccc;
background-color: #eee;
text-align: center;
padding: 20px 0; /* Up and down distance 20px about 0 */
}
#showmsg{
font-size: 25px;
}
.roll{
width: 880px;
height: 108px;
margin: 50px auto;
position: relative;
}
.roll ul{
position: absolute;
top: 0;
left: 0;
}
.roll li{
float: left;
width: 182px;
height: 108px;
}
.roll li a:hover{
position: relative;
top: 2px;
}
.roll .wrap{
width: 728px;
height: 108px;
position: relative;
margin: 0 auto;
overflow: hidden;
}
/* Left button */
.btn_left{
display: block;
width: 68px;
height: 68px;
background: url(../images/btn.jpg) no-repeat -70px -69px ;
position: absolute;/* // The absolute positioning will be off the mark Won't occupy a position */
top: 20px;
left: 1px;
z-index: 1;
}
.btn_left:hover{
background-position: -70px 0;
}
.btn_right{
width: 68px;
height: 68px;
background: url(../images/btn.jpg) no-repeat 1px -69px;
position: absolute;
right: 1px;
top: 20px;
z-index: 1;
}
.btn_right:hover{
background-position: 1px 0;
}js File configuration , Realize interactive animation effect
// Implement tab control scrolling
var g_bMoveLeft=true;
var g_oTimer = null;
var g_otimeout=null;
var g_bPause=true;
var g_iPauseTime =1000;
var g_iSpeed=2;
var arr = ['1','2','3','4','5','6','7','8'];
var index_arr = 0;
window.onload=function(){
var oDiv = document.getElementById('roll');
var oUl = oDiv.getElementsByTagName('ul')[0];
var aLi = oUl.getElementsByTagName('li');
var aA = oDiv.getElementsByTagName('a');
//var oMsg = document.getElementById('showmsg'); // Display information in the control panel
oUl.innerHTML=oUl.innerHTML+oUl.innerHTML; // towards ul Add content tags to tags
// alert(aLi.length);
// alert(aLi[0].offsetWidth);
//oUl.style.width='728px';// To give ul The label width because html There are no settings in the layout ul Any width of the label , Altitude information
oUl.style.width=aLi[0].offsetWidth*aLi.length+'px'; // Set up ul The width of the label
// alert(oUl.offsetLeft+' '+oUl.style.width);
for(var i=0;i<aLi.length;i++){
aLi[i].onmouseover=function(){
stopMove();
}
aLi[i].onmouseout=function(){
startMove(g_bMoveLeft);
}
};
// scroll left
aA[0].onmouseover=function(){
startMove(true);
}
aA[1].onmouseover=function(){
// stateOchk(this);
startMove(false);
};
startMove(g_bMoveLeft); // Default on timing
document.getElementById('showmsg').innerHTML=arr[0];//
// Rolling speed
oSeep.onchange=function(){
g_iSpeed=parseInt(this.value);
};
// Rolling time
oPauseTime.onchange=function(){
g_iPauseTime=parseInt(this.value);
}
}
// When the mouse moves in Img Destroy timer in tag
function stopMove(){
clearInterval(g_oTimer);
g_oTimer=null;
}
// Mouse removal div Turn on timer
function startMove(btnleft){
g_bMoveLeft=btnleft;
if(g_oTimer){// Destroy the timer if it exists
clearInterval(g_oTimer);
}
g_oTimer=setInterval(doMove,30);
}
function doMove(){
var oDiv = document.getElementById('roll'); // Get scrolling div
var oUl = oDiv.getElementsByTagName('ul')[0];
var ali = oUl.getElementsByTagName('li');// obtain li Element collection for
var lt = oUl.offsetLeft;
if(g_bMoveLeft){ //true Means to scroll left false Means to scroll right
lt-=g_iSpeed;
if(lt<-oUl.offsetWidth/2){// If left scrolling is less than ul Negative half [ If the width 12 commonly 6 (lt = -6+6=0 ) Equivalent to lt=0]
lt += oUl.offsetWidth/2;
}
}else{ // Scroll right
lt+=g_iSpeed;
if(lt>0){ // Default 0 0
lt=-oUl.offsetWidth/2;
}
};
if(g_bPause){ //true It means that the control can adjust the speed
if(Math.abs(lt-Math.round(lt/ali[0].offsetWidth)*ali[0].offsetWidth)<Math.ceil(g_iSpeed/2)){ // If the width of the picture is not enough to move
stopMove();// Destroy timer
g_otimeout=setTimeout(function(){// Turn on the delay timer
tabMsg();
startMove(g_bMoveLeft);
},g_iPauseTime);
lt=Math.round(lt/ali[0].offsetWidth)*ali[0].offsetWidth;
}
};
oUl.style.left=lt+'px';
}
// Switch picture text messages
function tabMsg(){
index_arr++;
// alert(index_arr);
if(index_arr>arr.length-1){
index_arr=0;
}
document.getElementById('showmsg').innerHTML=arr[index_arr];
}
The layout file shows htnl
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title></title>
<link rel="stylesheet" type="text/css" href="./css/style.css" />
<script src="js/style2.js"></script>
</head>
<body>
<div class="control"> <!-- Control panel -->
<span id="showmsg">22222</span>
</div>
<div class="roll" id="roll">
<a href="javascript:;" class="btn_left"></a>
<a href="javascript:;" class="btn_right"></a>
<div class="wrap">
<ul>
<li><a href="https://img0.baidu.com/it/u=3111055842,4088346805&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500" target="_parent"><img src="images/1.jpg" alt="1.jpg"></a></li>
<li><a href="https://img0.baidu.com/it/u=3111055842,4088346805&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500" target="_blank"><img src="images/2.jpg" alt="2.jpg"></a></li>
<li><a href="https://img0.baidu.com/it/u=3111055842,4088346805&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500" target="_search"><img src="images/3.jpg" alt="3.jpg"></a></li>
<li><a href="https://img0.baidu.com/it/u=3111055842,4088346805&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500" target="_top"><img src="images/4.jpg" alt="4.jpg"></a></li>
</ul>
</div>
</div>
</body>
</html>边栏推荐
- Go quiz: considerations for function naming return value from the go interview question (more than 80% of people answered wrong)
- What changes have taken place in the project file after SAP ui5 tools ran the Fiori add deploy config command
- Data7202 statistical analysis
- Analysis of IM project framework
- Learn the interface test, see it is very good, and make a note
- SAP ui5 beginner tutorial No. 27 - unit test tool quNit introduction trial version for SAP ui5 application
- 手机开户一般哪个证券公司好?手机开户是安全么?
- Introduction to sap ui5 tools
- Day22(File,DiGui,FileOutputStream)
- Day19 (variable parameter, enhanced for loop traversal, generic wildcard <? >, TreeSet, linkedhashset, nested traversal of sets, set set, static import,)
猜你喜欢

HashSet implementation class
Go language map sorting (key/value sorting)
Wind farm visualization: wind farm data

ERDAS 9.2 installation tutorial

Timed thread pool

Double recursion in deep analysis merge sort
Interviewer: what is an iterator? What is the relationship between async await and iterators?
SAP ui5 beginner tutorial 25 - using proxy server to solve the cross domain problem of SAP ui5 application accessing remote OData service trial version

JSON Library Tutorial from scratch (III): parsing strings, learning and sorting notes
SAP ui5 tutorial for beginners part XXVI - detailed steps for using OData service with mock server trial version
随机推荐
[pan Wai 1] Huawei computer test
SAP ui5 Application Development Tutorial Part 30 - parameter transfer in the routing process of SAP ui5
[OSPF routing calculation (class I LSA router, class II LSA network, and class III LSA sum net)] -20211228-30
Technology inventory: past, present and future of Message Oriented Middleware
Semantic segmentation cvpr2019-advance: advantageous enterprise minimization for domain adaptation in semantic segmentation
Some common errors and solutions of using SAP ui5 to consume OData services
Create a complete binary tree in array order
Semantic segmentation fcns in the wild: pixel level adaptive and constraint based adaptation
Go microservice development toolkit
C language - minesweeping
Use of pytorch tensorboard
First blog
SAP ui5 beginner tutorial No. 27 - unit test tool quNit introduction trial version for SAP ui5 application
Japanese fifty tone diagram
Synchonized introduction
SAP ui5 date type sap ui. model. type. Analysis of date parsing format
The e-book "action guide for large organizations to further promote zero code application platform" was officially released!
Day17 (set)
MySQL transaction learning notes (I) first encounter
SAP ui5 beginner tutorial No. 28 - Introduction to the integration test tool OPA for SAP ui5 applications