border流光效果,js通用,兼容ie7+,火狐,谷歌

html

<div id="box"></div>

css

#box{
     width: 200px;
     height: 200px;
     border:3px solid #eee;/* 必要条件 */
     position: absolute;/* 必要条件 */
     left:600px;
     top:200px;
}


此时页面上是一个边框为3px的#eee的200px的方形

js

$(‘#box‘).mouseover(function(){
    borderChange(‘#box‘,true,‘orange‘,1)
})
$(‘#box‘).mouseout(function(){
    borderChange(‘#box‘,false,‘orange‘,1)
})

borderChange()

// 通过js设置borderhover流光效果。
// 要求:1,hover元素需设置定位;
// 2,hover元素需设置border
var changeSwitch = false; //创建四个边框div的开关
var childW3,childH4,HoverW1;// 创建时第三个子div的宽,创建第四个子div的高,hover时第一个子div的宽
/*
borderChange鼠标hover元素时触发的函数
cName为hover元素的class名或Id名,格式例如:‘.box‘,‘#box‘,string
open:移入true,移出false,必填,Boolean
AfterColor:为移入时的边框颜色,必填,string
time就是动画执行时间单位s秒,格式例如:1s ,选填,number
timing_function是速度曲线,值详见https://www.runoob.com/cssref/css3-pr-transition-timing-function.html,选填,string
*/
function borderChange(cName,open,AfterColor,time,timing_function){
    if(time == undefined){
        time = 0.5;
    }
    if(timing_function == undefined){
        timing_function = ‘ease-in‘;
    }
    childW3 = parseInt(getStyle(cName,‘borderLeftWidth‘))*2+$(cName).width();
    childH4 = parseInt(getStyle(cName,‘borderLeftWidth‘))+$(cName).height();
    HoverW1 = parseInt(getStyle(cName,‘borderLeftWidth‘))+$(cName).width();
    HoverH2 = parseInt(getStyle(cName,‘borderLeftWidth‘))*2+$(cName).height();
    if(changeSwitch == false){
        var divChild = ‘‘;
        //上边
        divChild += ‘<div class="borderJS1" style="width: 0;height: 0;left:-‘+ getStyle(cName,‘borderLeftWidth‘) +‘;top:-‘+ getStyle(cName,‘borderLeftWidth‘) +‘;border-top:‘+ getStyle(cName,‘borderLeftWidth‘) +‘ solid ‘+ AfterColor +‘;position:absolute;"></div>‘
        //右边
        divChild += ‘<div class="borderJS2" style="width: 0;height: 0;left:‘+ $(cName).width() +‘px;top:-‘+ getStyle(cName,‘borderLeftWidth‘) +‘;border-left:‘+ getStyle(cName,‘borderLeftWidth‘) +‘ solid ‘+ AfterColor +‘;position:absolute;"></div>‘
        //下边
        divChild += ‘<div class="borderJS3" style="width:‘+ childW3+‘px;left:-‘+ getStyle(cName,‘borderLeftWidth‘) +‘;top:‘+ $(cName).height() +‘px;border-top:‘+ getStyle(cName,‘borderLeftWidth‘) +‘ solid ‘+ $(cName).css(‘borderLeftColor‘) +‘;position:absolute;"></div>‘
        //左边
        divChild += ‘<div class="borderJS4" style="height:‘+ childH4 +‘px;left:-‘+ getStyle(cName,‘borderLeftWidth‘) +‘;top:-‘+ getStyle(cName,‘borderLeftWidth‘) +‘;border-left:‘+ getStyle(cName,‘borderLeftWidth‘) +‘ solid ‘+ $(cName).css(‘borderLeftColor‘) +‘;position:absolute;"></div>‘
        changeSwitch = true;
        $(cName).append(divChild)
    }
    $(cName).css({
        ‘border-bottom‘:getStyle(cName,‘borderLeftWidth‘)+‘ solid ‘+ AfterColor +‘‘,
        ‘border-left‘:getStyle(cName,‘borderLeftWidth‘)+‘ solid ‘+ AfterColor +‘‘
    })
    if(open == true){
        setTimeout(function(){
            $(‘.borderJS1‘).css({‘width‘:HoverW1+‘px‘,‘transition‘:‘border-top ‘+ time +‘s ‘+ timing_function +‘ 0s,width ‘+ time +‘s ‘+ timing_function +‘ 0s‘})
            $(‘.borderJS2‘).css({‘height‘:HoverH2+‘px‘,‘transition‘:‘border-left ‘+ time +‘s ‘+ timing_function +‘ 0s,height ‘+ time +‘s ‘+ timing_function +‘ 0s‘})
            $(‘.borderJS3‘).css({‘width‘:‘0px‘,‘transition‘:‘width ‘+ time +‘s ‘+ timing_function +‘ 0s‘})
            $(‘.borderJS4‘).css({‘height‘:‘0px‘,‘transition‘:‘height ‘+ time +‘s ‘+ timing_function +‘ 0s‘,})
        },10)
    }else{
        setTimeout(function(){
            $(‘.borderJS1‘).css({
                ‘width‘: ‘0‘,
                ‘height‘: ‘0‘,
                ‘left‘:-getStyle(cName,‘borderLeftWidth‘),
                ‘top‘:-getStyle(cName,‘borderLeftWidth‘),
                ‘border-top‘:getStyle(cName,‘borderLeftWidth‘) +‘ solid ‘+ AfterColor +‘‘,
                ‘position‘:‘absolute‘
            })
            $(‘.borderJS2‘).css({
                ‘width‘: ‘0‘,
                ‘height‘: ‘0‘,
                ‘left‘:$(cName).width() +‘px‘,
                ‘top‘:-getStyle(cName,‘borderLeftWidth‘),
                ‘border-left‘:getStyle(cName,‘borderLeftWidth‘) +‘ solid ‘+ AfterColor +‘‘,
                ‘position‘:‘absolute‘
            })
            $(‘.borderJS3‘).css({
                ‘width‘:childW3+‘px‘,
                ‘left‘:-getStyle(cName,‘borderLeftWidth‘),
                ‘top‘:$(cName).height() +‘px‘,
                ‘border-top‘:getStyle(cName,‘borderLeftWidth‘) +‘ solid ‘+$(cName).css(‘borderRightColor‘),
                ‘position‘:‘absolute‘
            })
            $(‘.borderJS4‘).css({
                ‘height‘:childH4 +‘px‘,
                ‘left‘:-getStyle(cName,‘borderLeftWidth‘),
                ‘top‘:-getStyle(cName,‘borderLeftWidth‘),
                ‘border-left‘:getStyle(cName,‘borderLeftWidth‘) +‘ solid ‘+$(cName).css(‘borderRightColor‘),
                ‘position‘:‘absolute‘
            })
        },10)
    }
}

此时还不可以说做完了,需要做兼容,getStyle()函数在borderChange()函数中会使用

// 获取元素属性值的兼容写法
function getStyle(cName,attr){
    if(document.querySelector(cName).currentStyle){
        //IE,opera
        return document.querySelector(cName).currentStyle[attr];
    }else{
        //非IE,重点在于ff浏览器需要明确获取的元素属性,例如在google上获取,borderColor 在火狐上就得borderLeftColor
        console.log(getComputedStyle(document.querySelector(cName))[attr]);
        return getComputedStyle(document.querySelector(cName))[attr];
    }
}

现在就做好了一个效果单一,但是可以套用border流光效果。

原文地址:https://www.cnblogs.com/YvanNovEmotion/p/12016556.html

时间: 2024-08-03 21:44:25

border流光效果,js通用,兼容ie7+,火狐,谷歌的相关文章

css-dialog样式实现弹框蒙层全屏无需JS计算高度兼容IE7

<!DOCTYPE html><html><head>  <meta charset="UTF-8">  <title>css-dialog</title>  <script src="http://cdn.bootcss.com/jquery/1.9.1/jquery.min.js"></script> </head><style type="

iframe自适应高度(兼容IE 火狐 谷歌)

<div id="leamain"> <iframe src="#" marginheight="0" marginwidth="0" frameborder="0" scrolling="no" width="765" height=100% id="iframepage" name="iframepage"

常用的兼容IE和火狐FF等浏览器的js方法(js中ie和火狐的一些差别)

介绍了网页上常用的IE/火狐兼容性该页的做法,并给出了代码,相当实用了.为了方便大家阅读代码,以下以 IE 代替 Internet Explorer,以 MF/FF 代替 Mozzila Firefox .以下进入正题: //window.event IE:有window.event对象 FF:没有window.event对象.可以通过给函数的参数传递event对象.如onmousemove=doMouseMove(event) 解决方法:var event = event || window.

使用absolute模拟fixed定位,兼容ie6,及ie7 8 9和火狐谷歌等浏览器

ie6不支持fixed定位,设定fixed定位后,ie6会认为是错误值,便会使用默认值static,可以使用absolute模拟fixed效果,并且兼容ie 7 8 9以及火狐等. 核心代码:    html,body {        margin: 0;        padding: 0;        height: 100%;    }    html { overflow: hidden; }    body { overflow: auto;}    #demo { positio

Js 设置class,兼容ie,火狐的方式

var trs = document.getElementsByTagName("tr"); trs[0].className="color2";  //设置css样式. 不兼容火狐! 兼容火狐,ie的写法为: var cls = trs[0].getAttribute('class'); // 添加classtrs[0].setAttribute('class', cls + ' color3'); Js 设置class,兼容ie,火狐的方式,布布扣,bubuko

关于js的兼容问题(小办法)!

今天整理了一下浏览器对JS的兼容问题,希望能给你们带来帮助,我没想到的地方请留言给我,我再加上: 常遇到的关于浏览器的宽高问题: //以下均可console.log()实验 var winW=document.body.clientWidth||document.docuemntElement.clientWidth;//网页可见区域宽 var winH=document.body.clientHeight||document.docuemntElement.clientHeight;//网页可

兼容IE7音乐播放器之jplayer的使用

首先列出为何要写这篇随笔的原因: 1:兼容IE7 2:音乐播放器 3:任意控制播放器 1: 最近做的网站需要兼容IE7,在此之前已经写好了关于音乐播放的插件,火狐,IE8以上,以及谷歌浏览器等都可以随意播放,但是IE7以及IE8浏览器却无法正常播放,真是恼火至极啊!网上收了一大箩筐音乐播放器都不满足我的要求,基本上都是基于XML配置文件式的,而我的网站需要动态播放音乐,控制音乐单曲循环,我自定义的音乐列表顺序播放等,因此不能满足我的要求,这些播放器要么是html5的要么是纯flash的(无法控制

多行图片hover加边框兼容IE7+

问题: 遇到多行多列排列的图片时,hover上去加边框会把下面的图片挤到别处 ============================================================ 注意: IE11以下浏览器加载图片会默认给它加一个1px的边框 解决方法:对图片设置border:none ============================================================= hover加边框解决方案: 原先: <li> <img /&

css流光效果

css流光效果1: <!DOCTYPE html> <html> <head> <title>ww</title> </head> <style type="text/css"> /*CSS源代码*/ body{ background:#CFCFCF; } .image { position: relative; overflow:hidden; width: 300px; height: 450px;