Select with transition in change with onchange

Asked

Viewed 671 times

5

How to add a transition effect (fade) in a select with onchange, that when the object <div> change the same occurs a transition?
Below follows a print table that was made:

Print Tabela

And this is the script:

<meta charset="utf-8" />
<!-- Isto é a seleção de planos, basta adicionar em qualquer lugar para que o usuario selecione o plano 
Atenção! Em value coloque o valor do plano -->

<p> 
<select name="select" onchange="document.getElementById('selecionado').innerHTML = 'R$ ' + this.value;">  
     <option value="nenhum|" selected="selected">Selecione:</option>  
    <option value="10,00">Mensal</option>
    <option value="11,00">Trimestral</option>  
    <option value="12,00">Semestral</option>
    <option value="13,00">Anual</option>  
  </select>  
</p>  

<p id="selecionado">
    O valor padrão
</p>   <!-- Acrescente este código aonde você quer que o texto seja trocado -->

<!-- Como criar uma outra seleção:

Basta trocar o nome em:
<select name="select" onchange="document.getElementById('selecionado').innerHTML = 'R$ ' + this.value;">  
Este código você troca esta parte que estará em maiusculo:
<select name="select" onchange="document.getElementById('TROCAR ESTA PALAVRA PARA UMA OUTRA QUALQUER').innerHTML = 'R$ ' + this.value;"> 
E aí coloque o código de seleção:
<p id="selecionado"></p> 
Troque a palavra que "selecionado" para o mesmo que você colocou no primeiro código.
-->

I want when the person changes the payment cycle on select, he changes the price only with a fade, because the way it is, it changes on time. I want like a fade in this example: when clicking to change occurs a fade ( transition ) in prices, and not just changes.

Only on my chart it won’t be a single select, it will have several each plane with one, as shown in the image above.

  • Alexandre, you can use Mootools, jquery or other library?

  • Yes, the important thing is that it works. D

  • Hi, Alexandre, welcome to SOPT :) I edited your question to improve formatting, check out the markdown guide. Please always put the code with problems right here.

  • Thanks @brasofilo

2 answers

3


What you need is an animation. In the example below use jQuery Animate and I can soon put another variant with Mootools. I leave comments on the code.

Example:

mostrador.animate({
    opacity: 0           // mudar a opacidade para 0 = invisivel
}, 200, function () {    // chamar a função que corre quando o 1o animate acabar
    mostrador.html(val); // enquanto está invisivel mudar o valor
    mostrador.animate({
        opacity: 1      // mudar para visivel
    }, 200)             // velocidade = 200 milisegundos
})

jsFiddle

Test in this example to change the select.

To apply this to your code you need to make some changes:

  • If you want to use multiple selects I switched to have a div around each select to make it easy to find the element that shows the price. It can be done in other ways but it’s good not to customize too much.
  • I removed the ID of the <p id="selecionado"> ID’s have to be unique, I switched to a class: <p class="selecionado">
  • I removed the script inside the HTML, it is bad practice and used instead the $('select.selectPreco').on('change', function () {
  • But the problem is that I don’t understand much javascript, my strong one is CSS. Could you implement this function in the script I downloaded from above? #please

  • @Alexandrelopes, this is what you want: http://jsfiddle.net/7BN3P/ ?

  • Thanks a lot @Sergio, you helped me too!

  • My internet doesn’t help, I can’t access the site, it’s slow...

  • 1

    Yes yes, very much!!

  • @Alexandrelopes, it’s late for me, I have to go to sleep. If you need more help I’ll take a look here tomorrow morning. If this is what you want you can also accept the answer. But there are 1001 ways to do this. I left one of them as an example.

  • Our here in Brazil now that is 18:57 Thank you very much, I will test and come back here, damn, They even, you are a genius!

  • @Sergio could you add a new select? because here http://jsfiddle.net/7BN3P/ only has one, with two I could follow the structure of your code and make as many selects as I wanted without problems. : D

  • Below follows the template link of my table http://testetabela.esy.es/

  • @Sergio I tried here and I could not leave as the 1° and 3° plan, look there http://testetabela.esy.es/ I wanted it to stay as in the plans: Basic and Plus, :/

  • @Alexandrelopes, you’re out there?

  • @Alexandrelopes, after all you already had classes possible to use. Next time put your code in the question, you will get a better and faster answer! I leave here the code re-adapted. If you want we can have a chat about this to learn better without having to post 1001 comments here. But take a look at the code to see if you understand: http://jsfiddle.net/4n87D/

  • Add me in Skype please: Alexandre.Drs Sorry I just woke up, the Brazilian time zone is very different from yours, rsrsrs, and thank you very much for your patience! : D.... Yes this is what I wanted, man I don’t know how to thank you, you’re a genius!

  • @Alexandrelopes, we can use chat here in the OS. clicke here.

  • @Sergio, could you help me with this topic? http://answall.com/questions/14543/howto integrate!

Show 10 more comments

0

The site mentioned as example use the following script (adapted):

.find('.price-value').fadeOut('fast', function(){
    $(this).html('R$ ' + price_discount_split[0] +',<sup class="sup">' + 
    price_discount_split[1] + '</sup><span class="span">/mês</span>').fadeIn();
})

So if you want to use the same effect you can use the fadein + fadeOut jQuery.

1:

.find('.price-value').fadeOut(

disappears with the old value.

2:

'fast'

The first parameter tells the duration of the effect.

3:

function(){
    $(this).html('R$ ' + price_discount_split[0] +',<sup class="sup">' + 
    price_discount_split[1] + '</sup><span class="span">/mês</span>').fadeIn();
}

The second parameter is an Handler of a function to be executed when completing the fadeOut of the old price.

It then runs the fadein of the element itself, showing it again, but changing its content (.html()) - the new price is placed:

 $(this).html('R$ ' + novoPreco).fadeIn()
  • Thanks for the collaboration, I’ll also test your example.

  • you could host your example so I can see, as I said I don’t understand much of javascript.

  • 1

    André I’ve tried to use jQuery’s fadein and Out, but they don’t have a good transition to texts, and I want it to be for texts... Just like Sergio did.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.