Table of Contents
第一步编写HTML
第二步 JS代码准备工作
第三步 创建li元素
第四步 插入创建好的带有内容li标签.并且与第三步呼应,实现响应式瀑布流.
Home Web Front-end HTML Tutorial 瀑布流!而且还是响应式的哦!_html/css_WEB-ITnose

瀑布流!而且还是响应式的哦!_html/css_WEB-ITnose

Jun 24, 2016 am 11:17 AM

Untitled.gif

没错!这就是瀑布流!是不是很酷炫!看似很难实现,实则很简单!我是用li标签创建这些小块块,当然你也可以根据你的需要在li标签里,插入图片或者其他.下面我们就学习一下,如何实现网页的响应式瀑布流!

第一步编写HTML

可能会有小伙伴认为我们有20个块,需要写20个标签,那你就错了,在JS代码中我们可以利用for循环在HTML中查入10个.100个.1000个你想有多少就有多少!当然你最好提前为你的瀑布流做好css样式,我的这些小块都是由li标签创建的,这样在JS中创造的li标签直接赋予相应的类名.

<!DOCTYPE html><html>    <head>        <meta charset="UTF-8">        <title></title>        <style type="text/css">            * {                margin: 0;                padding: 0;            }            #flow {                list-style: none;                margin: 0 auto;                position: relative;            }            #flow li {                position: absolute;                background-color: #CCCCCC;                font-size: 50px;                width: 200px;                transition: all 0.5s;            }        </style>    </head>    <body>        <ul id="flow">            <!--<li>1</li>-->        </ul>    </body>
Copy after login

第二步 JS代码准备工作

这步中我们需要创建一个随机数,其函数的返回值我们用来定义每一个li元素的高度,当然如果你不想随机定义高度,那你就去写个数组吧!因为我的整体布局距离左边有10px距离,所以此处创建了一个leftSpace变量接受了一下.细心朋友可能已经发现了我的每一个元素期间都有10px的间距,所以此处创建了一个paddingSpace变量来限定每个li元素的间距.其他的在代码中都有相应的注释,自行理解!

<script type="text/javascript">        function ranH (min,max) {            return parseInt(Math.random()*(max-min+1))+min;        }        var flow = document.getElementById("flow");        // 左边距        var leftSpace = 10;        // 间距        var paddingSpace = 10;        //高度数组 保存每个li的高度        var hs = [];
Copy after login

第三步 创建li元素

由于在这步代码中,我们会看到出现bol值,而我们是在第四步定义了bol的值,并且根据bol的值来确定layout函数是实现创建li,还是更新li的位置.在这步我们可以先认为bol为true.首先,是创建li标签,然后获取屏幕的宽度根据屏幕的宽度设置一行有几个li标签,再根据li的标签的个数定义ul的宽度.在定义一下每列的li元素的高度,此处的作用是为了第四步的选出最短列而做准备.在给予每个li标签HTML内容,并且保存每个li的高度.

        //布局函数        //bol为真时 创建li        //bol为假时 更新li位置        function layout (bol) {            //获取所有的li 用于大小改变时 更新布局            var lis = document.getElementsByTagName("li");            var cols = parseInt(document.documentElement.clientWidth/200);                //ul宽度 跟随宽度一起变化            flow.style.width = cols*200 + 10*(cols-1)+20;            //初始化列高数组            var arrH =[];            for (var i = 0; i < cols; i++) {                arrH[i]=10;            }            function createLi (index) {                //获取已有的li或者新建                var li = lis[index]||document.createElement("li");                li.innerHTML = index;                //从数组中获取高度或者随机高度                var h = hs[index]||ranH(100,300);                li.style.height = h+"px";                if (bol) {                    hs.push(h);                }
Copy after login

第四步 插入创建好的带有内容li标签.并且与第三步呼应,实现响应式瀑布流.

首先求出最短列,将li元素插入最短列下,定义每一个li的位置.在最后利用系统的window.onresize 函数,当屏幕尺寸改变时执行layout()方法中,更新li位置的方法,以实现响应式瀑布流!

//求最短列                var minH = arrH[0];                var minI = 0;                for (var i = 0; i < cols; i++) {                    if (minH>arrH[i]) {                        minH=arrH[i];                        minI=i;                    }                }                //设置left                var  l = leftSpace+(200+paddingSpace)*minI;                li.style.left = l+"px";                //设置top                li.style.top = arrH[minI]+"px";                //添加                bol&&flow.appendChild(li);//                alert(li.offsetLeft+"~"+li.offsetTop+"~"+li.offsetWidth+"~"+li.offsetHeight);                //更新高度                arrH[minI] = arrH[minI] + li.offsetHeight +paddingSpace;            }            for (var i =0 ; i < 20 ;i++) {                createLi(i);            }        }        //创建        layout(true);        window.onresize =function  () {            //更新            layout(false);        }    </script></html>
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

The article discusses the HTML &lt;progress&gt; element, its purpose, styling, and differences from the &lt;meter&gt; element. The main focus is on using &lt;progress&gt; for task completion and &lt;meter&gt; for stati

What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

The article discusses the HTML &lt;datalist&gt; element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

What are the best practices for cross-browser compatibility in HTML5? What are the best practices for cross-browser compatibility in HTML5? Mar 17, 2025 pm 12:20 PM

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

The article discusses the HTML &lt;meter&gt; element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates &lt;meter&gt; from &lt;progress&gt; and ex

How do I use HTML5 form validation attributes to validate user input? How do I use HTML5 form validation attributes to validate user input? Mar 17, 2025 pm 12:27 PM

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

What is the viewport meta tag? Why is it important for responsive design? What is the viewport meta tag? Why is it important for responsive design? Mar 20, 2025 pm 05:56 PM

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

What is the purpose of the <iframe> tag? What are the security considerations when using it? What is the purpose of the <iframe> tag? What are the security considerations when using it? Mar 20, 2025 pm 06:05 PM

The article discusses the &lt;iframe&gt; tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Apr 04, 2025 pm 11:54 PM

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

See all articles