SVG 和 js 鼠标事件

布赖恩·霍斯特勒

我正在尝试使用带有鼠标事件的 SVG 并有一个问题。

当鼠标悬停在 SVG 的不同部分时,我正在使用鼠标悬停事件来更改背景颜色。这部分在我在这里制作的笔中工作:https : //codepen.io/brianne/pen/VGAgdW?editors=1010

但是,如果我将鼠标悬停在字母 C 或蓝色半圆内的小蓝色圆圈上,背景颜色会淡出然后淡入。当我将鼠标悬停在任何对象上时,我希望它保持青色背景颜色在那个组内。

任何人都可以指出我如何调整这个正确的方向吗?我的代码在下面以及 codepen 链接中。

提前致谢!

HTML

<div id="flowerbg" class=""></div>
<div class="container">
    <div class="framework-wrapper">
        <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 720 720">
            <g class="cls-1">
                <g id="" data-name="Layer 1">
                    <!-- Layer 5 / dark blue layer-->
                    <g class="layer layer5">
                        <a class="slice slice10" data-num="10">
                          <!--blue half circle-->
                            <path class="cls-2" d="M360,45C186,45,45,186,45,360l28.63-28.63a4,4,0,0,1,5.74,0L108,360H612l28.63,28.63a4,4,0,0,0,5.74,0L675,360C675,186,534,45,360,45Z" />
                            <!--small blue circle-->
                            <circle cx="250" cy="100" r="20" />
                            <!-- letter c -->
                               <path class="cls-8" d="M305.63,78.71l-1.28,1.34a5.33,5.33,0,0,0-4.86-1.31A5.79,5.79,0,0,0,302,90.06a5.26,5.26,0,0,0,3.87-3.19l1.71.75a7,7,0,0,1-5.21,4.17A7.56,7.56,0,1,1,299.11,77,6.9,6.9,0,0,1,305.63,78.71Z" />
                        </a>
                    </g>
                </g>
            </g>
        </svg>
    </div>
</div>

CSS

.bg10 {
    opacity:1 !important;
    background-color:teal;  
}
#flowerbg {
    width:100%;
    height:100%;
    position:absolute;
    top:0;
    transition: all .4s;
    opacity:1;
}

.container {
    position:relative;
    z-index:10;
    width:100vw;
    height:99vh;
    opacity:1;
}

.framework-wrapper {
    max-width:1000px;
    margin:0 auto;
    margin-top:0px;
    padding-top:60px;
}

//letters
.cls-8 {
    fill: white;
}

// all layers
.layer {
    cursor:pointer;
}

//all slices
.slice {
      path {
            transition: all .5s;
      }
}

.cls-2 {
    fill: navy;
}

.slice10 {
    circle {
        fill:blue;
    }
}

JS

$(document).ready(function(){

//detect when mouse stops moving function
var timeout;
$(document).on('mousemove', function (event) {
    if (timeout !== undefined) {
        window.clearTimeout(timeout);
    }
    timeout = window.setTimeout(function () {
        $(event.target).trigger('mousemoveend');
    }, 100);
});

var lastBg = '';

$('.slice').mousemove(function(e){
    //once the mouse stops moving
    $('.slice').on('mousemoveend', function () {

        // get slide number
        var num = $(this).data('num');

        //first mousemovement set flower bg 
        if (lastBg == "") {
            lastBg = num;
            var flower = 'bg' + num;
            $("#flowerbg").fadeIn("slow", function() {
                $(this).addClass(flower);
            });

        }
        //if the last background that was hovered is the same as the current one 
        else if (lastBg == num) {
             var flower = 'bg' + num;
            $("#flowerbg").fadeIn("slow", function() {
                $(this).addClass(flower);
            });
        }
        //if it's not the first mouse movement and the last bg doesn't equal the currently hovered one 
        else {

            lastBg = num;
            var flower = 'bg' + num;
            $("#flowerbg").fadeIn("slow", function() {
                $(this).addClass(flower);
            });

        }

    });
}); 

$(".framework-wrapper").mouseout(function(evt) {
    $("#flowerbg").fadeOut("fast", function() {
        $("#flowerbg").removeClass();
    });
});

});

罗伯特·朗森

在您不想对鼠标做出反应的任何内容上设置指针事件:无。

$(document).ready(function(){

//detect when mouse stops moving function
var timeout;
$(document).on('mousemove', function (event) {
    if (timeout !== undefined) {
        window.clearTimeout(timeout);
    }
    timeout = window.setTimeout(function () {
        $(event.target).trigger('mousemoveend');
    }, 100);
});

var lastBg = '';

$('.slice').mousemove(function(e){
    //once the mouse stops moving
    $('.slice').on('mousemoveend', function () {

        // get slide number
        var num = $(this).data('num');

        //first mousemovement set flower bg 
        if (lastBg == "") {
            lastBg = num;
            var flower = 'bg' + num;
            $("#flowerbg").fadeIn("slow", function() {
                $(this).addClass(flower);
            });

        }
        //if the last background that was hovered is the same as the current one 
        else if (lastBg == num) {
             var flower = 'bg' + num;
            $("#flowerbg").fadeIn("slow", function() {
                $(this).addClass(flower);
            });
        }
        //if it's not the first mouse movement and the last bg doesn't equal the currently hovered one 
        else {

            lastBg = num;
            var flower = 'bg' + num;
            $("#flowerbg").fadeIn("slow", function() {
                $(this).addClass(flower);
            });

        }

    });
}); 

$(".framework-wrapper").mouseout(function(evt) {
    $("#flowerbg").fadeOut("fast", function() {
        $("#flowerbg").removeClass();
    });
});

});
.bg10 {
    opacity:1 !important;
    background-color:teal;  
}
#flowerbg {
    width:100%;
    height:100%;
    position:absolute;
    top:0;
    transition: all .4s;
    opacity:1;
}

.container {
    position:relative;
    z-index:10;
    width:100vw;
    height:99vh;
    opacity:1;
}

.framework-wrapper {
    max-width:1000px;
    margin:0 auto;
    margin-top:0px;
    padding-top:60px;
}

//letters
.cls-8 {
    fill: white;
}

// all layers
.layer {
    cursor:pointer;
}

//all slices
.slice {
      path {
            transition: all .5s;
      }
}

.cls-2 {
    fill: navy;
}

.slice10 {
    circle {
        fill:blue;
    }
}

circle, .cls-8 {
  pointer-events: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="flowerbg" class=""></div>
<div class="container">
    <div class="framework-wrapper">
        <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 720 720">
            <g class="cls-1">
                <g id="" data-name="Layer 1">
                    <!-- Layer 5 / dark blue layer-->
                    <g class="layer layer5">
                        <a class="slice slice10" data-num="10">
                          <!--blue half circle-->
                            <path class="cls-2" d="M360,45C186,45,45,186,45,360l28.63-28.63a4,4,0,0,1,5.74,0L108,360H612l28.63,28.63a4,4,0,0,0,5.74,0L675,360C675,186,534,45,360,45Z" />
                            <!--small blue circle-->
                            <circle cx="250" cy="100" r="20" />
                            <!-- letter c -->
                               <path class="cls-8" d="M305.63,78.71l-1.28,1.34a5.33,5.33,0,0,0-4.86-1.31A5.79,5.79,0,0,0,302,90.06a5.26,5.26,0,0,0,3.87-3.19l1.71.75a7,7,0,0,1-5.21,4.17A7.56,7.56,0,1,1,299.11,77,6.9,6.9,0,0,1,305.63,78.71Z" />
                        </a>
                    </g>
                </g>
            </g>
        </svg>
    </div>
</div>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

带有显示的奇怪鼠标事件:flex和svg

来自分类Dev

React.js,Rx.js和鼠标事件

来自分类Dev

React.js,Rx.js和鼠标事件

来自分类Dev

使用Fabric.js启用和禁用画布的鼠标事件

来自分类Dev

D3.js div阻止SVG中的鼠标悬停事件

来自分类Dev

鼠标事件和块元素

来自分类Dev

鼠标事件和块元素

来自分类Dev

Snap.svg捕获鼠标滚轮事件

来自分类Dev

样式和鼠标与 SVG 元素的交互

来自分类Dev

将鼠标悬停在d3.js中的链接和节点事件上应显示文本

来自分类Dev

Perl6:NCurses和鼠标事件

来自分类Dev

欺骗,渲染循环和鼠标事件

来自分类Dev

按钮和鼠标事件之间的区别

来自分类Dev

滚动和鼠标事件动画标题

来自分类Dev

带有表和鼠标事件的jQuery

来自分类Dev

骚扰,渲染循环和鼠标事件

来自分类Dev

jQuery鼠标事件和滑块问题

来自分类Dev

Forge Viewer 中的鼠标和按键事件

来自分类Dev

使用svg.js加载和转换并嵌入SVG文件

来自分类Dev

Angular.js和SVG生成指令

来自分类Dev

使用SVG和JS进行绘画

来自分类Dev

Raphael.js的鼠标事件由任何鼠标按钮触发

来自分类Dev

<svg>元素在鼠标事件期间更新位置时无法移动

来自分类Dev

svg元素的“显示”属性如何影响鼠标事件?

来自分类Dev

在多个重叠的SVG元素上检测鼠标事件

来自分类Dev

D3:通过叠加的SVG检测鼠标滚轮事件

来自分类Dev

如何将(鼠标)事件添加到SVG的XMLDocument

来自分类Dev

无法在 Angular 7 中的 SVG 上触发鼠标事件

来自分类Dev

如何区分滚轮单击事件和鼠标按下事件?