单击时禁用JavaScript mouseenter / mouseleave

时间:2016-06-27 13:43:35

标签: javascript html

对于我的网站,我有一个图像,在mouseenter / mouseleave上更改它的源并切换gylphicon。我想这样做,如果单击glypicon,mouseenter / mouseleave功能将不再运行。点击后,图片不应在悬停时更改状态,只能在点击时更改。

我的HTML:

 <div id="mySplash1Mobile" class="carousel-splash slide" data-ride="carousel">
  <div class="carousel-inner" role="listbox">
    <div class="item active">
      <img src="img/index-splash/1main.jpg" onmouseenter="hover1(this);" onmouseleave="unhover1(this);" id="ImageButton1" width="100%">
    </div>
    </div>
    <a class="glyphicon glyphicon-triangle-bottom glyphicon-read-more" href="javascript:click1();" id="GlyphiconButton1">
      <span class="sr-only">More</span>
    </a>
  </div>

我的JavaScript:     var hoverState = true;

//Image Hover Blur
if (hoverState == true)
{
  function hover1(element) {
      element.setAttribute('src', 'img/index-splash/1blur.jpg');
      $('#GlyphiconButton1').toggleClass("glyphicon-triangle-top");
  }
  function unhover1(element) {
      element.setAttribute('src', 'img/index-splash/1main.jpg');
      $('#GlyphiconButton1').toggleClass("glyphicon-triangle-top");
  }

//Image Glyphicon Click Read More
var image_tracker1 = 'main';

function click1() {
  var image = document.getElementById('ImageButton1');
  if(image_tracker1=='main'){
    image.src = 'img/index-splash/1blur.jpg';
    $('#GlyphiconButton1').toggleClass("glyphicon-triangle-top");
    image_tracker1 = 'blur';
  }else{
    image.src = 'img/index-splash/1main.jpg';
    $('#GlyphiconButton1').toggleClass("glyphicon-triangle-top");
    image_tracker1 = 'main';
  }
  hoverState = false;
}

1 个答案:

答案 0 :(得分:0)

尝试将if(hoverState)放在函数中:

var hoverState = true;

//Image Hover Blur
function hover1(element) {
   if (hoverState) {
      element.setAttribute('src', 'img/index-splash/1blur.jpg');
      $('#GlyphiconButton1').toggleClass("glyphicon-triangle-top");
   }
}

function unhover1(element) {
   if (hoverState) {
      element.setAttribute('src', 'img/index-splash/1main.jpg');
      $('#GlyphiconButton1').toggleClass("glyphicon-triangle-top");
   }
}

//Image Glyphicon Click Read More
var image_tracker1 = 'main';

function click1() {
  var image = document.getElementById('ImageButton1');
  if(image_tracker1=='main'){
    image.src = 'img/index-splash/1blur.jpg';
    $('#GlyphiconButton1').toggleClass("glyphicon-triangle-top");
    image_tracker1 = 'blur';
  }else{
    image.src = 'img/index-splash/1main.jpg';
    $('#GlyphiconButton1').toggleClass("glyphicon-triangle-top");
    image_tracker1 = 'main';
  }
  hoverState = false;
}