天来分享一个JavaScript图片跟随导航栏特效
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
*{margin: 0; padding: 0;}
ul {list-style:none;}
body {
background-color: #000;
}
.nav {
width: 800px;
height: 42px;
background:url("images/rss.png") no-repeat right center #fff;
margin: 100px auto;
border-radius: 5px;
position: relative;
}
.cloud {
width: 83px;
height: 42px;
position: absolute;
top: 0;
left: 0;
background: url(images/cloud.gif) no-repeat;
}
.nav ul {
position: absolute;
top: 0;
left: 0;
}
.nav li {
float: left;
width: 83px;
height: 42px;
line-height: 42px;
text-align: center;
color: #000;
cursor: pointer;
}
</style>
</head>
<body>
<div class="nav" id="nav">
<span class="cloud" id="cloud"></span>
<ul>
<li>首页新闻</li>
<li>师资力量</li>
<li>活动策划</li>
<li>企业文化</li>
<li>招聘信息</li>
<li>公司简介</li>
<li>上海校区</li>
<li>广州校区</li>
</ul>
</div>
</body>
</html>
<script>
var cloud = document.getElementById("cloud"); // 云彩
var nav = document.getElementById("nav");
var lis = nav.children[1].children;
var current = 0; // 用于存放点击时候的 offsetLeft
//alert(lis.length);
for(var i=0; i<lis.length;i++) {
lis[i].onmouseover = function() {
// alert(this.offsetLeft);
target = this.offsetLeft; // 把左侧的位置 ,给 target
}
lis[i].onmouseout = function() {
target = current; // 鼠标离开 target 是 刚才我们点击的位置
}
lis[i].onclick = function() {
current = this.offsetLeft; // 点击的时候 吧当前位置 存贮一下
}
}
// 缓动公式
var leader = 0, target = 0;
setInterval(function(){
leader = leader + (target - leader ) / 10;
cloud.style.left = leader + "px";
},10);
</script>
今天的分享就到这里了,大家看完后学会了吗?
如果觉得文章对您有用就加个关注啦~~
欢迎评论留言,提供建议和思路。
HTML标题通过<h1>-<h6>
标题大小与字体大小的关系:1到6号标题与1到6号字体是逆序对应
实例:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>文档标题</title>
</head>
<body>
<h1>标题1</h1>
<h2>标题2</h2>
</body>
</html>
HTML段落是通过标签<p>来定义
实例:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>文档标题</title>
</head>
<body>
<p>段落1</p>
<p>段落2</p>
</body>
</html>
HTML链接是通过标签<a>来定义的
实例:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>文档标题</title>
</head>
<body>
<a href = "https://www.baidu.com"> 这是一个链接</a>
</body>
</html>
HTML通过标签<img>来定义
实例:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>文档标题</title>
</head>
<body>
<img src="/images/logo.png" width="258" height="39" />
</body>
</html>
大多数HTML元素的属性:
属性 | 描述 |
class | 为html元素定义一个或多个类名(classname)(类名从样式文件引入) |
id | 定义元素的唯一id |
style | 规定元素的行内样式(inline style) |
title | 描述了元素的额外信息 (作为工具条使用) |
<hr>标签在HTML页面中创建水平线
hr元素可用于分隔内容
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>文档标题</title>
</head>
<body>
<p>hr 标签定义水平线:</p>
<hr />
<p>这是段落。</p>
<hr />
<p>这是段落。</p>
<hr />
<p>这是段落。</p>
</body>
</html>
运行结果:
可以将注释插入 HTML 代码中,这样可以提高其可读性,使代码更易被人理解。浏览器会忽略注释,也不会显示它们。
注释写法如下:
<!-- 这是一个注释 -->
作用是向网页中添加图片,并且img标签有多个可用参数可以添加。
*请认真填写需求信息,我们会在24小时内与您取得联系。