zoukankan      html  css  js  c++  java
  • 例如筋斗云的效果,但不通过offset定位的flag标记

    效果:mouseenter到li上出现背景图片,mouseleave后背景图片消失,click以后该背景图片被锁定

    问题:简单的mouseenter,mouseleave和click事件不能达到预期的效果,因为当click事件结束后,同时也会触发mouseleave事件

    解决方案:我们给每一个li一遍循环,给li动态添加flag属性,默认值为true,是这样的我们判断当flag为false给该li设置背景图片,为true给清空;所以当mouseenter的时候,给移入的li设置背景图片;当鼠标点击li的时候给li的flag设置为取反,而其余兄弟li的flag均赋予true的属性,给点击的li设置背景图片;最后通过判断flag来决定是否触发mouseleave事件,循环遍历li,如果li的flag为true的背景图片清空

    代码示例,不完全针对这个,有html结构的差别

      

    $(function () {
    $li = $(".gywm-content .content-main-left li");
    $($li[0]).find("a")
    .css({
    "background": "url('image/guanyuwomen_anniu_xuanzhogn.png')"
    , "color": " #ff2826"
    });
    $li.each(function (index, ele) {
    ele.flag = true;
    })
    $li.mouseenter(function () {
    $li.index = $(".gywm-content .content-main-left li").index($(this));
    $(this).find("a").css({
    "background": "url('image/guanyuwomen_anniu_xuanzhogn.png')"
    , "color": " #ff2826"
    });
    });
    $li.click(function () {
    $(this).siblings().each(function (index, ele) {
    ele.flag = true;
    });
    this.flag = !this.flag;
    $li.index = $(".gywm-content .content-main-left li").index($(this));
    $(".gywm-content .content-main-right li").removeClass("show");
    $(".gywm-content .content-main-right li").eq($li.index).addClass("show");
    $(this).find("a").css({
    "background": "url('image/guanyuwomen_anniu_xuanzhogn.png')"
    , "color": " #ff2826"
    });
    })
    $li.mouseleave(function () {
    $li.each(function (index, ele) {
    if (ele.flag) {
    $(ele).find("a")
    .css({
    "background": ""
    , "color": " #333333"
    });
    }
    })
    });
    })
  • 相关阅读:
    洛谷 P1026 [NOIP2001 提高组] 统计单词个数
    CodeForces
    CodeForces
    常用的正则表达式(复制粘贴即可)
    Vue生命周期
    前端开发面试题
    前端Vue中动态使用本地图片路径
    《剑指offer》面试题13 在O(1)时间删除链表节点 Java版
    《剑指offer》面试题12 打印1到最大的n位数 Java版
    《剑指offer》面试题11 数值的整数次方 Java版
  • 原文地址:https://www.cnblogs.com/QIQIZAIXIAN/p/6196716.html
Copyright © 2011-2022 走看看