javascript - 如何定义单击 div 时将在两个函数中使用的变量值?

标签 javascript jquery html rotation click

我正在尝试制作一些东西,您可以单击箭头按钮突出显示一个框并显示相应的文本,也可以单击该框以显示相应的文本。

我可以使用箭头按钮,但不确定如何合并单击框以显示文本的功能。

现在,箭头按钮的函数有一个名为“now”的变量,该变量设置为 0。单击该框时必须重新定义该变量,以便箭头按钮仍然有效。

var p = $('.text > p');
var rect = $('.rectangles > svg');
var now = 0;

p.hide().first().show();
rect.css("opacity",".3").first().css("opacity","1")

$("#next").click(function (e) {
  p.eq(now).hide();
  rect.eq(now).css("opacity",".3")
  now = (now + 1 < p.length) ? now + 1 : 0;
  p.eq(now).show();
  rect.eq(now).css("opacity","1")
  });

$("#prev").click(function(e) {
  p.eq(now).hide();
  now = (now > 0) ? now - 1 : p.length - 1;
  p.eq(now).show();
});
svg {
  width: 100px;
  height: 50px;
}

rect:hover {
  fill: black !important;
  cursor: pointer;
}

#rect1 {
  fill: orange;
  width: 100px;
  height: 50px;
}

#rect2 {
  fill: teal;
  width: 100px;
  height: 50px;
}

#rect3 {
  fill: violet;
  width: 100px;
  height: 50px;
}

a {
  text-decoration: none;
  display: inline-block;
  padding: 8px 16px;
  margin-bottom: 2em;
}

a:hover {
  background-color: gray;
  color: black;
}

#prev, #next {
  background-color: #e5e5e5;
  color: black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#" id="prev">&#8249;</a>
<a href="#" id="next">&#8250;</a>

<Br>

<div class="rectangles">
  <svg>
    <rect id="rect1" />
  </svg>

  <svg>
    <rect id="rect2" />
  </svg>

  <svg>
    <rect id="rect3" />
  </svg>
</div>

<div class="text">
  <p id="text1" class="text">Text 1</p>
  <p id="text2" class="text">Text 2</p>
  <p id="text3" class="text">Text 3</p>
</div>

这是 fiddle 链接:https://jsfiddle.net/Lfjyawtd/

最佳答案

您在 rect 上定义一个点击事件,并使用 eq() 获取 rect 的当前索引,并相应地更改其不透明度:

var p = $('.text > p');
var rect = $('.rectangles > svg');
var now = 0;

p.hide().first().show();
rect.css("opacity", ".3").first().css("opacity", "1")

$("#next").click(function(e) {
  p.eq(now).hide();
  rect.eq(now).css("opacity", ".3")
  now = (now + 1 < p.length) ? now + 1 : 0;
  p.eq(now).show();
  rect.eq(now).css("opacity", "1")
});

$("#prev").click(function(e) {
  p.eq(now).hide();
  now = (now > 0) ? now - 1 : p.length - 1;
  p.eq(now).show();
});

$('rect').on('click', function() {
  rect.css("opacity", ".3")
  rect.eq($(this).index("rect")).css('opacity', '1');
  p.hide();
  p.eq($(this).index("rect")).show();
});
svg {
  width: 100px;
  height: 50px;
}

rect:hover {
  fill: black !important;
  cursor: pointer;
}

#rect1 {
  fill: orange;
  width: 100px;
  height: 50px;
}

#rect2 {
  fill: teal;
  width: 100px;
  height: 50px;
}

#rect3 {
  fill: violet;
  width: 100px;
  height: 50px;
}

a {
  text-decoration: none;
  display: inline-block;
  padding: 8px 16px;
  margin-bottom: 2em;
}

a:hover {
  background-color: gray;
  color: black;
}

#prev,
#next {
  background-color: #e5e5e5;
  color: black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#" id="prev">&#8249;</a>
<a href="#" id="next">&#8250;</a>

<Br>

<div class="rectangles">
  <svg>
    <rect id="rect1" />
  </svg>

  <svg>
    <rect id="rect2" />
  </svg>

  <svg>
    <rect id="rect3" />
  </svg>
</div>

<div class="text">
  <p id="text1" class="text">Text 1</p>
  <p id="text2" class="text">Text 2</p>
  <p id="text3" class="text">Text 3</p>
</div>

关于javascript - 如何定义单击 div 时将在两个函数中使用的变量值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48420004/

相关文章:

java - 如何在 Apache tomcat 服务器启动时定期运行 javascript 函数而不是使用 ScheduledExecutorService 的 java 类?

java - 使用 Hibernate 在 ArrayList 中进行 Ajax 响应

javascript - Fancybox 1.3.4 无法正常工作

html - 我可以将本地静态 html 页面重定向到在线资源吗?

jquery - 在网页中悬停时弹出

javascript - 在 JavaScript 事件之间传递参数的简洁方法?

javascript - 如何在用户单击饼图时隐藏其余区域?

javascript - 在 JavaScript 中创建的模态不会在移动设备上消失

javascript - 切换 mousemove mousestop 事件

html - 在同一行上使用 CSS 省略号和其他元素