简体   繁体   English

使用javascript最小化和最大化文本

[英]minimize and maximize the text using javascript

I wrote the code for minimizing and maximizing the text when we click the symbol. 单击该符号时,我编写了用于最小化和最大化文本的代码。 I have tried the following: 我尝试了以下方法:

html code: html代码:

<div style="font:12px verdana;background:#fefcd9;padding:10px 10px 10px 10px;border:solid 1px lightgray;" id="text">
Instructions<span id="min" style="cursor:pointer;float:right;">[+]</span><span id="max" style="cursor:pointer;float:right;">[-]</span><br/><br/>
Perhaps you should look for someone who will dig deep to learn about you, your business, your services and products. A writer who will put herself in the shoes of your potential customers in order to answer these important questions:hhhhhhhh
</div>

javascript: javascript:

<script type="text/javascript">
$('#text').click(function(){
 $("#min").toggle(200);
    $("#max").toggle(200);
});
</script>

In the above the first div has to display "instructions" and the "[+]". 在上面的第一个div中必须显示“指令”和“ [+]”。 When I click the "[+]" symbol it has to maximize the div and display all the remaining text. 当我单击“ [+]”符号时,它必须最大化div并显示所有剩余的文本。 Also the [+] symbol has to be replaced with a [-] symbol, which will minimize the text. 同样,[+]符号也必须替换为[-]符号,这将最小化文本。

Can anyone help me? 谁能帮我?

Ok I think this jsFiddle will answer your question! 好的,我认为这个jsFiddle会回答您的问题!

I used jQuery UI core because with it you can do very cool animation like stuff with your elements, and basically you need two functions for each button (trust me this is the best way) and gave one of the buttons display: none; 我使用了jQuery UI核心,因为有了它,您可以执行非常酷的动画,例如使用元素进行填充,并且基本上每个按钮都需要两个函数(相信我,这是最好的方法),并且display: none;了一个按钮display: none; so when it toggles the [-] and [+] change places like you wanted. 因此,当它切换[-]和[+]时,可以根据需要更改位置。

So the js part: 因此,js部分:

$(".tr").click(function() {
    $(".bc").toggle("blind");
    $(".tr").find("span").toggle();
});

The html part: html部分:

<div class="container clearfix" id="text">
    <div class="tl">Instructions</div><div class="tr"><span class="min-button">[-]</span><span class="max-button" style="display: none;">[+]</span><br/><br/></div>
    <div class="bc">
        Perhaps you should look for someone who will dig deep to learn about you, your business, your services and products. A writer who will put herself in the shoes of your potential customers in order to answer these important questions:hhhhhhhh</div>
</div>

And the css part: 和CSS部分:

.container{
    margin-top: 40px;
    font:12px verdana;
    background:#fefcd9;
    padding:10px 10px 10px 10px;
    border:solid 1px lightgray;
    width: 400px;
}

.container .tl{
    position: relative;
    float: left;
}

.container .tr{
    position: relative;
    float: right;
}

.tr .min-button{
    cursor:pointer;
    float:left;
}

.tr .max-button{
    cursor:pointer;
    float:left;
}

.container .bc{
    position: relative;
    display: inline-block;
    float: right;
}

.clearfix:after
{
    content: ".";
    display: block;
    clear: both;
    visibility: hidden;
    line-height: 0;
    height: 0;
}
.clearfix
{
    display: inline-block;
}

You probably want to use slideToggle() not toggle() 您可能想使用slideToggle()而不是toggle()

The text to reveal should also be hidden at first 首先,还应隐藏要显示的文本

try this: 尝试这个:

http://jsfiddle.net/VvkSZ/ http://jsfiddle.net/VvkSZ/

HTML: HTML:

<div style="font:12px verdana;background:#fefcd9;padding:10px 10px 10px 10px;border:solid 1px lightgray;" >Instructions
    <span id="min" style="cursor:pointer;float:right;">[-]</span>
    <br/><br/>
    <div id="text">
Perhaps you should look for someone who will dig deep to learn about you, your business, your services and products. A writer who will put herself in the shoes of your potential customers in order to answer these important questions:hhhhhhhh
    </div>
</div>​

JS: JS:

$('#min').click(function(){
 $("#text").toggle(200);    
});​

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM